为什么 Xml Serializer 要求属性可写?
我有一些类型想要序列化为 xml,但这些类型具有只读属性,例如:
public List<Effect> Effects {get; private set;}
但 xml 序列化程序要求这些属性可写。
xml 序列化程序不是使用反射,因此实际上可以通过反射轻松设置这些属性,即使它们是只读的?
有没有办法解决这个问题,因为我不希望这些类型可以被人们编辑,所以属性必须是只读的,但我也希望它们是可 xml 可序列化的。
I have some types I want to serialize as xml, but these types have read-only properties like:
public List<Effect> Effects {get; private set;}
but the xml serializer requires these properties to be writable.
Isn't xml serializer using reflection, so in effect can easily set these properties via reflection even though they are read-only?
Is there a way around this because I don't want these types to be editable by people, so the properties must be read-only, but I also want them to be xml serializeable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是不可能的,因为正如 MSDN 中提到的
但您可以使用 DataContractSerializer。 的答案的链接
这是 Marc 关于 SO 序列化私有成员数据
更新
您可以通过保留自动实现的属性来克服这种行为,并具有如下内容:
Its not possible because As mentioned in MSDN
But you can use DataContractSerializer. Here is a link to Marc's Answer on SO
Serializing private member data
Update
You can get over that behavior by leaving Auto Implemented properties and have somthing like this: