继承的类属性中的属性差异

绿眼的安迪

我有两节课:ElementDisplay

现在这两个类都具有许多公共属性,因此我考虑从基类继承。

现在出现了问题。对于序列化,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
{
}
CodeCaster

您可以使基类和属性抽象化,并在需要时为每个派生类设置属性:

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章