Ask a questionAsk a question
 

AnswerField attribute target for auto-properties

  • Friday, October 12, 2007 1:52 PMorangy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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...

Answers

  • Monday, October 15, 2007 10:49 PMMarcelo Guerra - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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

All Replies

  • Saturday, October 13, 2007 1:14 AMMarcelo Guerra - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi,

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

     

    Regards,

     

    Marcelo.

  • Sunday, October 14, 2007 8:04 AMorangy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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.

  • Monday, October 15, 2007 10:49 PMMarcelo Guerra - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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