设置DataContract和DataMember没有所有的属性 [英] Set DataContract and DataMember Without All the Attributes

查看:441
本文介绍了设置DataContract和DataMember没有所有的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到 [DataContract] [数据成员] 属性有点乱,宁愿做这样的代码在配置方法什么的。这可能吗?

I find the [DataContract] and [DataMember] attributes a bit messy and would rather do this with code in a config method or something. Is this possible?

推荐答案

您不必在所有使用这些属性。 的DataContractSerializer 将与序列化的getter和setter,但与序列化,你会很容易地异常结束导航属性的实体的情况下,所有的公共属性因循环引用。

You don't have to use these attributes at all. DataContractSerializer will serialize all public properties with getter and setter but in case of serializing entities with navigation properties you will easily end with exception due to "cyclic reference".

要避免这种情况的例外,你必须使用 [DataContract(IsReference = TRUE)] 在你的实体类数据成员您要serilize每个属性或 IgnoreDataMember 你不想序列化的每个属性。

To avoid that exception you must either use [DataContract(IsReference = true)] on your entity class with DataMember on every property you want to serilize or IgnoreDataMember on every property you don't want to serialize.

最后也是最复杂的选择是避免完全的属性和自定义类实施 IDataContractSurrogate 来控制型之外序列化。

The last and the most complex option is avoiding attributes completely and custom classes implementing IDataContractSurrogate to control serialization outside of the type.

您也可以写你完全自定义的序列化过程中,或者使用XML序列化或二进制序列化的一切需要。

You can also write your completely custom serialization process or use XML serialization or binary serialization with all its requirements.

这篇关于设置DataContract和DataMember没有所有的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆