继承类属性中的属性差异

Gre*_*ndy 3 c#

我有两个班:ElementDisplay.

现在这两个类都有许多共同的属性,所以我考虑从一个基类继承.

现在出现了问题.对于序列化,Element类应该[XmlIgnore]在每个属性之前具有,但Display不是类.

我该怎么处理?

旧:

public class Element
{
    [Browsable(false)]
    [XmlIgnore]
    public Brush BackgroundBrush { get; set }
}

public class Display
{
    public Brush BackgroundBrush { get; set }
}
Run Code Online (Sandbox Code Playgroud)

新:

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
{
}
Run Code Online (Sandbox Code Playgroud)

Cod*_*ter 5

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

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; }
}
Run Code Online (Sandbox Code Playgroud)