Go C#. NET how to Not serialize fields, properties

Source: Internet
Author: User

C#. NET how to Not serialize fields, properties
  • When we use public properties and public fields, we are able to serialize them smoothly.

    View Sourceprint?01.[Serializable]02.public class MyClass03.{04.    public int ID;05. 06.    public string Address;07. 08.    private int _age;09. 10.    public string Name { get; set; }11. 12.    public int Age13.    {14.        get { return _age; }15.        set { _age = value; }16.    }17.}

    The result of XML serialization into a file is like this:

    Json

    Bin because the bin file is invisible to the naked eye, so it is represented by deserialization

    But there will always be fields or properties that you don't want to save as files.

    JSON and XML do not want to be serialized "Properties" and "fields" using the following attribute:

    [System.Xml.Serialization.XmlIgnore] [System.Web.Script.Serialization.ScriptIgnore] BinaryFormatter If you do not want to be serialized "attribute" Just use the following in the corresponding "field": [NonSerialized] Look at an example:

    View Sourceprint?01.[Serializable]02.public class MyClass03.{04.    [NonSerialized]05.    public int ID;06. 07.    public string Address;08. 09.    [NonSerialized]10.    private int _age;11. 12.    public string Name { get; set; }13. 14.    [System.Xml.Serialization.XmlIgnore]15.    [System.Web.Script.Serialization.ScriptIgnore]16.    public int Age17.    {18.        get { return _age; }19.        set { _age = value; }20.    }21.}

    XML serialization, ignoring the Age property

    JSON serialization, ignoring the Age property

    BinaryFormatter serialization ignores the Age property and ID field

Go C#. NET how to Not serialize fields, properties

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.