Set The “NonSerializedAttribute” To An Auto Property

前端 未结 7 2333
青春惊慌失措
青春惊慌失措 2021-02-18 18:59

This cannot be done in C#. Any way to do it?

...

laugh, in case my little pun wasn\'t understood, what I mean is: how can I mark a property in C# as Non

7条回答
  •  离开以前
    2021-02-18 19:17

    What was said above is right: You can't prevent an auto-implemented property from being serialized by setting an attribute like [NonSerialized]. It just does not work.

    But what does work is the [IgnoreDataMember] attribute in case you are working with an WCF [DataContract]. So

    [DataContract]
    public class MyClass
    {
        [DataMember]
        public string ID { get; set; }
    
        [IgnoreDataMember]
        public string MySecret { get; set; }
    }
    

    will get serialized under WCF.

    Although since WCF is an opt-in technology, you can also just omit the [IgnoreDataMember] and it will work as well. So maybe my comment is a little academical ;-)

提交回复
热议问题