Assuming we have two classes Apple, Pineapple
public class Apple:Fruit{}
public class Pineapple:Fruit{}
And we have an abstract class named Fruit
[XmlInclude(typeof(Apple))]
[XmlInclude(typeof(Pineapple))]
public abstract class Fruit{}
And we have a class named Menu
public class Menu
{
[XmlElement("apple",typeof(Apple))]
[XmlElement("",typeof(Pineapple))]
public Fruit fruit {get;set;}
}
I'd like to ignore the fruit property when the type is Pineapple.
Not sure why you would want to do that, but you can use the ShouldSerialize<PropertyName>
pattern to achieve it:
public class Menu
{
[XmlElement("apple",typeof(Apple))]
public Fruit fruit {get;set;}
public bool ShouldSerializefruit()
{
return !(fruit is Pineapple);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.