我有两节课: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; }
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句