专用字段的C#DataContract属性? [英] C# DataContract Attribute for Private Fields?

查看:194
本文介绍了专用字段的C#DataContract属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于标有[DataContract]属性的类,应将其应序列化的私有字段标记为[DataMember]吗?

For a class marked with the attribute, [DataContract], does its private fields, which should be serialized, be marked as [DataMember]?

示例:

[DataContract]
public class Component
{

// [DataMember] is not needed since public fields get automatically serialized
public int number;

// do I need [DataMember] here?
private string characters;

// [DataMember] is required here, but I also need to include the 
// attribute [DataMember] in this class's definition
private complexType cT;

我正在正确阅读 DataContractAttribute类,对吧?

I'm reading DataContractAttribute Class correctly, right?

推荐答案

不,您似乎没有正确阅读文档.

No, it doesn't look like you are reading the documentation correctly.

DataContracts是一种公开共享信息的方法,与常规序列化有点不同.

DataContracts are a way to publicly share information that is a little different than regular serialization.

在文档页面中,您链接:

From the documentation page you link:

然后必须将DataMemberAttribute属性应用于数据协定类型的每个成员,以表明它是数据成员,也就是说,应该对其进行序列化.

The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized.

但这仅适用于像您一样添加[DataContract]属性的情况.

But that only applies if you add the [DataContract] attribute like you did.

这篇关于专用字段的C#DataContract属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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