为具有“DataContract”属性的类创建的重复属性和“可串行化”

发布于 2024-10-09 21:30:50 字数 358 浏览 0 评论 0原文

我有一个带有 2 个属性的类,“DataContract”和“Serializable”。此类正在服务中使用。该服务正在被一个项目使用。我使用VS的“添加服务引用”功能在项目中添加了服务的引用(右键单击引用--添加--服务引用)。每当我添加服务的引用时,都会在服务引用的 Reference.cs 文件中创建该类的重复属性,其中 1 个属性具有“DataMember”属性,其他属性具有“Serializable”属性。这会导致构建错误。如果我不添加 Serialized 属性,则服务引用只需使用具有“DataMember”属性的类的 1 组属性即可正常工作。

知道为什么会发生这种情况吗?我们可以采取什么措施来避免这种情况?请注意,这两个属性都是该类所必需的,即我无法删除任何属性。

I have a class with 2 attributes on it, "DataContract" and "Serializable". This class is being used in a service. This service is being consumed by a project. I have added the reference of the service in the project by using the "add service reference" functionality of VS (Right Click on references -- Add -- Service Reference). Whenever I add the reference of the service, duplicate properties of the class is created in the Reference.cs file of the service reference, 1 with "DataMember" attribute and other with "Serializable" attribute. This is causing a build error. If I do not put the Serializable attribute, the service reference works fine with just 1 set of properties of the class with "DataMember" attribute.

Any idea why is this happening and what can we do to avoid this? Please note that both the attributes are required for the class i.e. I cannot remove any of the attributes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

骄兵必败 2024-10-16 21:30:50

您能解释一下为什么需要这两个属性吗?

DataContractSerializer 可以与 Serialized 属性配合使用。

但是,要使 BinaryFormattor 工作,您需要 Serialized 属性。

找到一种方法来摆脱 DataContract 属性。

--编辑--

WCF 默认使用 DataContractSerializer 通过通道序列化/反序列化对象。根据 MSDN 文档,

@ DataContractSerializer 类

// You must apply a DataContractAttribute or SerializableAttribute
// to a class to have it serialized by the DataContractSerializer.

因此,为了使 WCF 正常工作,您可以摆脱 DataContract

Can you explain why you need both the attributes?

DataContractSerializer works fine with Serializable attribute.

But, for BinaryFormattor to work, you need Serializable attribute.

Find a way to get rid of DataContract attribute.

--EDIT--

WCF uses DataContractSerializer by default to serialize/deserialize objects over channels. According to MSDN documentation,

@ DataContractSerializer Class

// You must apply a DataContractAttribute or SerializableAttribute
// to a class to have it serialized by the DataContractSerializer.

So, for WCF to work, you can get rid of DataContracts.

注定孤独终老 2024-10-16 21:30:50

@Samar最好的方法是仅使用 DataContract 属性,因为它在内部使 Serialized 属性进入图片(默认情况下)。

@Samar The best way would be to use only DataContract attribute, as it internally makes the Serializable attribute come in to picture (by default).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文