我有两个班:Element和Display.
现在这两个类都有许多共同的属性,所以我考虑从一个基类继承.
现在出现了问题.对于序列化,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)
您可以使基类和属性成为抽象,并在需要时为每个派生类设置属性:
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)
| 归档时间: |
|
| 查看次数: |
54 次 |
| 最近记录: |