为什么XmlSerializer不需要将类型标记为[Serializable]? [英] Why doesn't the XmlSerializer need the type to be marked [Serializable]?
问题描述
在C#中,如果我想使用 XmlSerializer
序列化一个实例,则对象的类型不必标记为 [Serializable]
属性。但是,对于其他序列化方法,例如 DataContractSerializer
,需要将该类标记为 [Serializable]
或 [DataContract]
。
In C#, if I want to serialize an instance with XmlSerializer
, the object's type doesn't have to be marked with [Serializable]
attribute. However, for other serialization approaches, such as DataContractSerializer
, needs the class be marked as [Serializable]
or [DataContract]
.
关于序列化要求是否存在任何标准或模式?
Is there any standard or pattern about serialization requirement?
推荐答案
这是因为XmlSerializer仅序列化公共字段/属性。其他形式的序列化可以序列化私有数据,这构成了潜在的安全风险,因此您必须使用属性选择加入。
This is because XmlSerializer only serializes public fields/properties. Other forms of serialization can serialize private data, which constitutes a potential security risk, so you have to "opt in" using an attribute.
这篇关于为什么XmlSerializer不需要将类型标记为[Serializable]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!