假设我们有两个类Apple,Pineapple
public class Apple:Fruit{}
public class Pineapple:Fruit{}
我们有一个名为Fruit
的抽象类[XmlInclude(typeof(Apple))]
[XmlInclude(typeof(Pineapple))]
public abstract class Fruit{}
我们有一个名为Menu
的类public class Menu
{
[XmlElement("apple",typeof(Apple))]
[XmlElement("",typeof(Pineapple))]
public Fruit fruit {get;set;}
}
当类型为Pineapple时,我想忽略fruit属性。
答案 0 :(得分:1)
不确定为什么要这样做,但您可以使用ShouldSerialize<PropertyName>
模式来实现它:
public class Menu
{
[XmlElement("apple",typeof(Apple))]
public Fruit fruit {get;set;}
public bool ShouldSerializefruit()
{
return !(fruit is Pineapple);
}
}