使用 .NET 的 XmlSerializer 时忽略派生类的属性
我有一个带有虚拟属性的基类和一个重写虚拟属性的派生类型。该类型可以序列化为 XML。我想做的不是当对象属于派生类型时保留项目属性列表。为了实现此目的,派生类使用 [XmlIgnore]
属性来修饰重写的属性。基类中的虚拟属性不应用 XmlIgnore
属性。由于某种原因,即使对象属于派生类型 (DynamicCart
),项目列表也会被序列化。
当我将 XmlIgnore 属性应用于基类中的虚拟属性时,列表不会序列化到文件。
public class ShoppingCart
{
public virtual List<items> Items{get; set;}
//and other properties
public void SerializeToXML (string filePath)
{
var xmlSerializer = new XmlSerializer(this.GetType());
textWriter = new System.IO.StreamWriter(filePath);
xmlSerializer.Serialize(textWriter, this);
textWriter.Flush();
textWriter.Close();
}
}
//A cart that is populated by algo based on parameters supplied by user. I have no need to
//persist the actual items across sessions.
class DynamicCart: ShoppingCart
{
[XmlIgnore]
public override List<items>{get;set;}
//and other properties
}
class Shop
{
ShoppingCart cart = new DynamicCart();
PopulateCart(cart);
cart.serializeToXML(<PATH TO FILE>);
}
I have a base class with a virtual property and a derived type that overrides the virtual property. The type can be serialized to XML. What I am trying to do is NOT to persist the List of items property when the object is of the derived type. To acheive this the derived class decorates the overridden property with the [XmlIgnore]
attribute. The virtual property in the base class does NOT apply XmlIgnore
attribute. For some reason the List of items get serialized every even when the object is of the derived type (DynamicCart
).
When I apply XmlIgnore
attribute to the virtual property in the base class the list does not get serialized to file.
public class ShoppingCart
{
public virtual List<items> Items{get; set;}
//and other properties
public void SerializeToXML (string filePath)
{
var xmlSerializer = new XmlSerializer(this.GetType());
textWriter = new System.IO.StreamWriter(filePath);
xmlSerializer.Serialize(textWriter, this);
textWriter.Flush();
textWriter.Close();
}
}
//A cart that is populated by algo based on parameters supplied by user. I have no need to
//persist the actual items across sessions.
class DynamicCart: ShoppingCart
{
[XmlIgnore]
public override List<items>{get;set;}
//and other properties
}
class Shop
{
ShoppingCart cart = new DynamicCart();
PopulateCart(cart);
cart.serializeToXML(<PATH TO FILE>);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过向基类添加虚拟
ShouldSerialize***
方法来实现此目的。例如:这会产生(稍微整理一下):
该方法必须在
ShouldInclude...
之后包含要考虑的属性的确切名称。You can do this by adding a virtual
ShouldSerialize***
method to the base class. For example:This produces (tidied slightly):
The method must have include the exact name of the property to consider after
ShouldInclude...
.我认为您的序列化程序正在使用您的基类而不是派生类。
I think your serializer is using your base class instead of the derived.
如果我理解得很好,您要做的就是有损转换为基类,也称为 切片(至少在 C++ 中)。
如果您有能力将课程变成 records (我相信是这种情况),代码非常简单,因为您可以使用基础的合成复制构造函数。
其余代码仍然保留。看一下稍微修改过的演示,在工作中看到它。
If I understand well, what you're trying to do is a lossy conversion to base class also called slicing (at least in C++).
If you can afford to turn your classes into records (which is the case, I believe), the code is pretty straightforward, as you can use the synthesized copy constructor of the base.
The rest of the code remains. Take a look at a slightly modified demo to see it at work.
我想您需要在基类中声明派生类型以进行 XML 序列化。听起来有点傻,但这是按照规范的。
请参阅此 MSDN 页面,并查找以下示例:
I guess you need to declare the derived type in the base class for XML serialization. Sounds a bit silly, but is by specification.
See this MSDN page, and look for the following example:
尝试一下,
这将允许您添加尽可能多的类型,您希望序列化程序包含在内。
Try this
this will allow you to add as many types, you want serializer to inclusde.