我有两个类:Element
和 Display
。
现在这两个类都有很多共同的属性,所以我考虑从一个基类继承。
问题来了。对于序列化,Element
类应该在每个属性之前有 [XmlIgnore]
,但 Display
类没有。
我该如何处理?
旧的:
public class Element
{
[Browsable(false)]
[XmlIgnore]
public Brush BackgroundBrush { get; set }
}
public class Display
{
public Brush BackgroundBrush { get; set }
}
新:
public class DispBase
{
public Brush BackgroundBrush { get; set; }
}
public class Element : DispBase
{
// what to write here, to achieve XmlIgnore and Browsable(false)?
}
public class Display : DispBase
{
}
最佳答案
您可以使基类和属性抽象化,并在需要时为每个派生类设置属性:
public abstract class DispBase
{
public abstract Brush BackgroundBrush { get; set; }
}
public class Element : DispBase
{
[XmlIgnore]
[Browsable(false)]
public override Brush BackgroundBrush { get; set; }
}
public class Display : DispBase
{
public override Brush BackgroundBrush { get; set; }
}
关于c# - 继承类属性中的属性差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31243445/