How can I prevent a datamember from being serialized

后端 未结 5 1192
鱼传尺愫
鱼传尺愫 2021-01-12 16:58

I only want only to de-serializing a certain data member, without serializing it.

I understand I can set EmitDefaultValue =false, and set the value to null.

<
5条回答
  •  长发绾君心
    2021-01-12 17:34

    Which serializer?If this is XmlSerializer then either:

    public int Foo {get;set;}
    [XmlIgnore]
    public bool FooSpecified {
        get { return false; } // never serialize
        set { }
    }
    

    or

    public int Foo {get;set;}
    public bool ShouldSerializeFoo() { return false; }
    

    will do this. A quick test shows that this doesn't work for DataContractSerializer, though. protobuf-net also supports both of these, for info.

提交回复
热议问题