C#:在制作 class [Serializable] 时,哪些成员将被序列化为 XML 元素
C#: What members will be serialized to XML element when making a class [Serializable]
给定一个标记为[Serializable]
的class,我们如何知道它的成员是否会被序列化(该成员没有属性 ) XmlSerializer
?
例如:
[Serializable]
public class C2
{
public int x1 = 1;
private int x2 = 2;
public static int x3 = 3;
public readonly int x4 = 4;
public int Y1 { get; set; }
public static int Y2 { get; set; }
}
我们有一个classC2
,将一个新对象C2 c = new C2()
序列化成XML字符串后,发现只有x1
和Y1
被序列化。所以我推断:
public
字段和 属性 将被序列化。
private
字段和 属性 将不会被序列化。
static
和 readonly
字段和 属性 将不会被序列化。
- MethodInfo 不会被序列化。
- ...
我的问题是,是否有任何指导方针可以知道:没有为 class 的成员标记任何属性,我们如何知道该成员是否会被序列化?
由于 XML 序列化不使用 SerializableAttribute
,因此在 class(请参阅 What is [Serializable] and when should I use it? 了解使用它的原因)。
您似乎已经正确地获得了从 Introducing XML Serialization:
序列化的内容列表
XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information.
给定一个标记为[Serializable]
的class,我们如何知道它的成员是否会被序列化(该成员没有属性 ) XmlSerializer
?
例如:
[Serializable]
public class C2
{
public int x1 = 1;
private int x2 = 2;
public static int x3 = 3;
public readonly int x4 = 4;
public int Y1 { get; set; }
public static int Y2 { get; set; }
}
我们有一个classC2
,将一个新对象C2 c = new C2()
序列化成XML字符串后,发现只有x1
和Y1
被序列化。所以我推断:
public
字段和 属性 将被序列化。private
字段和 属性 将不会被序列化。static
和readonly
字段和 属性 将不会被序列化。- MethodInfo 不会被序列化。
- ...
我的问题是,是否有任何指导方针可以知道:没有为 class 的成员标记任何属性,我们如何知道该成员是否会被序列化?
由于 XML 序列化不使用 SerializableAttribute
,因此在 class(请参阅 What is [Serializable] and when should I use it? 了解使用它的原因)。
您似乎已经正确地获得了从 Introducing XML Serialization:
序列化的内容列表XML serialization serializes only the public fields and property values of an object into an XML stream. XML serialization does not include type information.