C# - 跳过基类属性的序列化?

时间:2018-01-26 20:11:23

标签: c# inheritance datacontractserializer datamember

我搜索了一会儿但没有找到答案。

我正在使用 DataContractSerializer 。是否可以跳过基本属性的序列化?

基类:

[KnownType(typeof(DerivedClass))]
[DataContract]
public class BaseClass
{
    [DataMember(IsRequired = false)]
    public virtual int BaseProperty{ get; set; }

    //...
    //Many other properties with [DataMember]

}

派生类:

[DataContract]
public class DerivedClass : BaseClass
{
    // *** NO [DataMember] here***
    public override int BaseProperty
    {
        get
        {
            throw new NotSupportedException("Does not apply");          
        }
        set
        {
            throw new NotSupportedException("Does not apply");          
        }
    }
}

在这种情况下,我看到错误消息:

System.Runtime.Serialization.SerializationException: 'Error in line 1 position 248...Expecting element 'BaseProperty'.

0 个答案:

没有答案