我在基础中有一个抽象属性,它被 2 个派生类覆盖。我希望为其中一个序列化属性,而不是另一个:
[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;
}
}
}
我认为将 [DataMember] 属性放在 Leaf 的“IsSelected”属性上就足够了,但显然,连 Leaf 的属性都没有被序列化。
谢谢!