How can I prevent a datamember from being serialized

£可爱£侵袭症+ 提交于 2020-03-14 10:48:51

问题


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.

But I also do not want to change the value of the datamember, is there any other way of achieving this?

The serializer is DataContractSerializer. :)

Thanks.


回答1:


You can change the value of the data member before the serialization (to the default value, so it doesn't get serialized), but then after the serialization you'd change it back - using the [OnSerializing] and [OnSerialized] callbacks (more information in this blog post). This works fine as long as you don't have multiple threads serializing the object at the same time.

public class StackOverflow_8010677
{
    [DataContract(Name = "Person", Namespace = "")]
    public class Person
    {
        [DataMember]
        public string Name;
        [DataMember(EmitDefaultValue = false)]
        public int Age;

        private int ageSaved;
        [OnSerializing]
        void OnSerializing(StreamingContext context)
        {
            this.ageSaved = this.Age;
            this.Age = default(int); // will not be serialized
        }
        [OnSerialized]
        void OnSerialized(StreamingContext context)
        {
            this.Age = this.ageSaved;
        }

        public override string ToString()
        {
            return string.Format("Person[Name={0},Age={1}]", this.Name, this.Age);
        }
    }

    public static void Test()
    {
        Person p1 = new Person { Name = "Jane Roe", Age = 23 };
        MemoryStream ms = new MemoryStream();
        DataContractSerializer dcs = new DataContractSerializer(typeof(Person));
        Console.WriteLine("Serializing: {0}", p1);
        dcs.WriteObject(ms, p1);
        Console.WriteLine("   ==> {0}", Encoding.UTF8.GetString(ms.ToArray()));
        Console.WriteLine("   ==> After serialization: {0}", p1);
        Console.WriteLine();
        Console.WriteLine("Deserializing a XML which contains the Age member");
        const string XML = "<Person><Age>33</Age><Name>John Doe</Name></Person>";
        Person p2 = (Person)dcs.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(XML)));
        Console.WriteLine("  ==> {0}", p2);
    }
}



回答2:


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.




回答3:


Have you tried decorating the property with [IgnoreDataMember]?




回答4:


There is the attribute System.Xml.Serialization.XmlIgnoreAttribute wich say to xmkserializers to ignore your property. But it only change xml serialization behavior.




回答5:


add the IgnoreDataMemberAttribute



来源:https://stackoverflow.com/questions/8010677/how-can-i-prevent-a-datamember-from-being-serialized

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!