我在基类中有一个抽象属性,被两个派生类覆盖。其中一个派生类需要将该属性序列化,而另一个则不需要:
[Serializable]
[DataContract(IsReference = true)]
public abstract class Component
{
public abstract bool IsSelected { get; set; }
}
[Serializable]
[DataContract]
public class Leaf : Component
{
[DataMember]
public override bool IsSelected { get; set; } // serialized
}
[Serializable]
[DataContract]
public class Composite : Component
{
private List<Component> componentList = new List<Component>();
/// <summary>
/// Should *not* be serialized
/// </summary>
public override bool IsSelected
{
get
{
foreach (Component component in componentList)
{
if (!component.IsSelected)
return false;
}
return true;
}
set
{
foreach (Component component in componentList)
component.IsSelected = value;
}
}
}
我认为在Leaf的'IsSelected'属性上放置[DataMember]属性就足够了,但显然,甚至Leaf的属性都没有被序列化。
谢谢!