我可以同时使用DataContract和Serializable吗? [英] Can I use DataContract and Serializable together?

查看:151
本文介绍了我可以同时使用DataContract和Serializable吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理WCF服务.我的所有类都已经使用[Serializable]属性进行了序列化,但是由于"k__BackingField"属性命名问题,我使用了DataContract和DataMember属性. 所以我可以像下面这样一起使用这两个属性吗?

I am working on WCF service. My all class are already serialize using [Serializable] attribute but due to "k__BackingField" Property Naming problem I used DataContract and DataMember attribute. so Can i use both attribute together like following:

[Serializable]
[DataContract]
public class User
{

  [DataMember]
  public string Name { get; set; }

  [DataMember]
  public int UserID { get; set; }
}

这正确吗?

我在这里也有类似的解决方案. JSON的C#自动属性反序列化

I also got similar solution here. C# automatic property deserialization of JSON

可序列化和DataContract(不与?)

推荐答案

我据此在MSDN上找到了一篇文章,我们可以同时使用DataContract属性和Serializable属性.

I found an article on MSDN according to this we can use both attribute DataContract and Serializable together.

使用[可序列化],所有字段都成为数据协定的一部分(除非它们被标记为[NonSerialized]).在[DataContract]中,仅包括标有[DataMember]的成员.请注意,如果一个类型同时具有[DataContract]和[Serializable]属性,它将使用[DataContract]映射

With [Serializable], all fields become part of the data contract (unless they are marked with [NonSerialized]). With [DataContract], only members marked with [DataMember] are included. Note that if a type has both [DataContract] and [Serializable] attributes on it, it will use the [DataContract] mapping

http://msdn.microsoft.com/en-us/magazine/cc163569.aspx

这篇关于我可以同时使用DataContract和Serializable吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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