locked
Field attribute target for auto-properties RRS feed

  • Question

  • I wonder, why "field" attribute target is not supported for automatic properties, but supported (since 1.1 AFAIR) for events?

    I mean something like this:

     

    Code Block

     

    [field: NonSerialized]

    public string AutoProperty { get; set; }

     

     

    Looks like oversight...

    Friday, October 12, 2007 1:52 PM

Answers

  • Hi,

    Now I understand Smile.

     

    This behavior is "by design". The decision at the time auto-properties were implemented was that they would work in the "common case" which among other things means no attributes on the generated field. The idea behind that is keeping them simple and not slowly mutating them into full properties.

    So, if you need to use the NonSerialized attribute full properties are the way.

     

    If you disagree please feel free to open a connect issue on this.

     

    Regards,

     

    Marcelo

    Monday, October 15, 2007 10:49 PM

All replies

  • Hi,

    For properties you need to use [property: ...] instead of field.

     

    Regards,

     

    Marcelo.

    Saturday, October 13, 2007 1:14 AM
  • No, I don't mean default target. If you have event like this:

    Code Block

     

    [field: NonSerialized]

    public event EventHandler Closed;

     

     

    NonSerializedAttribute is attached to the field generated by compiler. I'd like to have the same possibility with automatic properties.

    Sunday, October 14, 2007 8:04 AM
  • Hi,

    Now I understand Smile.

     

    This behavior is "by design". The decision at the time auto-properties were implemented was that they would work in the "common case" which among other things means no attributes on the generated field. The idea behind that is keeping them simple and not slowly mutating them into full properties.

    So, if you need to use the NonSerialized attribute full properties are the way.

     

    If you disagree please feel free to open a connect issue on this.

     

    Regards,

     

    Marcelo

    Monday, October 15, 2007 10:49 PM