Set The “NonSerializedAttribute” To An Auto Property

前端 未结 7 2358
青春惊慌失措
青春惊慌失措 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

    I theory yes, it's possible. In practical nope, not possible.

    Serialization classes only works on private fields. When you define a auto property; at behind the scenes compiler automatically generates a private field for it. That means this is a language feature not a .net framework feature.

    Also serialization classes are included in redbits, which is any change prohibited due compatibility except bug fixes.

    I hope thats helps.

    0 讨论(0)
  • 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 ;-)

    0 讨论(0)
  • 2021-02-18 19:21

    For events, you can use [field:NonSerialized], but for auto-properties this does not work. It seems like it would be a very logical way to handle auto-properties as well, but for some reason it doesn't seem to have been implemented.

    0 讨论(0)
  • 2021-02-18 19:23
      [NonSerialized]
      public string MyProperty { get; set; }
    

    Is an error

      [XmlIgnore]
      public string MyProperty { get; set; }
    

    Is not an error

    NonSerialized Indicates that a field of a serializable class should not be serialized.

    XmlIgnore Instructs the Serialize method of the XmlSerializer not to serialize the public field or public read/write property value

    so, if you ask

    I would expect to have some way to allow me to prevent their serialization.

    the answer yes, if you're using XmlSerializer

    0 讨论(0)
  • 2021-02-18 19:25

    You could probably do this with Mono.Cecil, a bytecode manipulation library. Theoretically, you could add custom attributes to the hidden backing field. This is so inconvenient, however, that I don't think it warrants an example.

    If you had a large application with your own postprocessor, you might consider creating your own substitute for NonSerializedAttribute that could be applied to properties. The postprocessor could then use Mono.Cecil or similar to apply NonSerializedAttribute to the backing fields. It's quite common for large applications to undergo such postprocessing to save that extra bit of typing.

    0 讨论(0)
  • 2021-02-18 19:31

    Edit * : Auto Implemented Properties are backed by an anonymous field which you don't really have access to, attributes are designed to be controlled by a reflection based mechanism. These fields cannot be referenced by the reflection mechanism (because they are anonymous). This compiler feature would require a lot of changes to the generation of auto-properties... It would also require that the compiler treat auto-properties as fields for the purpose of marking field attributes onto them.

    To answer the more fundamental part of the question - your point was that Auto-Properties are serialized and so there should be a way to control their serialization. You're right - but auto properties are meant as a shorthand and were never designed to give you the full flexibility, but rather to allow you to easily extend their functionality the "long" way if you ever needed it.

    • I added the more details answer from my comments to the body of the answer.
    0 讨论(0)
提交回复
热议问题