如何忽略实例的属性被序列化?

时间:2018-01-25 09:16:53

标签: c# serialization xml-serialization datacontractserializer

我正在使用 DataContractSerializer 进行序列化/反序列化。我需要忽略一个被序列化的实例的特定属性,该属性在另一个类中用作属性。在这里,我为您的参考提供了简单的代码结构。我尝试了 IgnoreDataMember XmlIgnore 属性,但这些都没有用。

[DataContract(Name ="Sample")]
public class Sample1
{
    [DataMember]
    public Sample2 s2 { get; set; }
}

[KnownType("KnownTypes")]
public class Sample2
{
    [DataMember]
    public Sample3 s3 { get; set; }
}
public class Sample3
{
    public string property1 { get; set; }

    public string property2 { get; set; }

    //No need to serialize this property while serializing "S3" property in "Sample2" class.
    //Is it possible???
    public string property3 { get; set; }
}

0 个答案:

没有答案