如何在 C# 中忽略参数序列化
我有一个需要在 C# 中序列化的对象,但是该对象具有以下属性:
[XmlElement("NodeConfiguration")]
public NodeConfigurationSerialize NodeConfiguration { get; set; }
在序列化期间必须忽略该属性,并且在反序列化期间必须考虑该属性,换句话说,我需要以这样的方式配置该属性:仅被序列化过程忽略。
有谁知道这是否可能?如果是这样,我该怎么办?
注意:我使用的是 Visual Studio 2010 和 .NET Framework 4.0。
I have an object that I need to serialize in C#, however this object has the property:
[XmlElement("NodeConfiguration")]
public NodeConfigurationSerialize NodeConfiguration { get; set; }
This property must be ignored during the serialization and must be considered during the deserialization, in other words I need to configure this property in such a way that it is only ignored by the serialization process.
Does anyone know if this is possible? If so, how can I do it?
Note: I am using the Visual Studio 2010 and the .NET Framework 4.0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的设计需要进行一些小改动。您需要某种方式来表示“未设置”:
当您不想设置它时,将其设置为“null”,当您这样做时,您可以将其设置为您想要的任何值。现在您可以自动使用序列化/反序列化,而无需手动编辑文件。
I think small change in your design is needed. You need some way to say 'this isn't set':
When you don't want it set, set it to 'null', when you do, you can set it to whatever you want. Now you can automatically use serialization/deserialization without needing to manually edit the files.
您可以实现 System.Xml.Serialization.IXmlSerialized 接口:
You could implement the
System.Xml.Serialization.IXmlSerializable
interface: