Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
72 lines (51 sloc) 2.41 KB

Auto-Implemented Property Field-Targeted Attributes

Summary

This feature intends to allow developers to apply attributes directly to the backing fields of auto-implemented properties.

Motivation

Currently it is not possible to apply attributes to the backing fields of auto-implemented properties. In those cases where the developer must use a field-targeting attribute they are forced to declare the field manually and use the more verbose property syntax. Given that C# has always supported field-targeted attributes on the generated backing field for events it makes sense to extend the same functionality to their property kin.

Detailed design

In short, the following would be legal C# and not produce a warning:

[Serializable]
public class Foo 
{
    [field: NonSerialized]
    public string MySecret { get; set; }
}

This would result in the field-targeted attributes being applied to the compiler-generated backing field:

[Serializable]
public class Foo 
{
    [NonSerialized]
    private string _mySecretBackingField;
    
    public string MySecret
    {
        get { return _mySecretBackingField; }
        set { _mySecretBackingField = value; }
    }
}

As mentioned, this brings parity with event syntax from C# 1.0 as the following is already legal and behaves as expected:

[Serializable]
public class Foo
{
    [field: NonSerialized]
    public event EventHandler MyEvent;
}

Drawbacks

There are two potential drawbacks to implementing this change:

  1. Attempting to apply an attribute to the field of an auto-implemented property produces a compiler warning that the attributes in that block will be ignored. If the compiler were changed to support those attributes they would be applied to the backing field on a subsequent recompilation which could alter the behavior of the program at runtime.
  2. The compiler does not currently validate the AttributeUsage targets of the attributes when attempting to apply them to the field of the auto-implemented property. If the compiler were changed to support field-targeted attributes and the attribute in question cannot be applied to a field the compiler would emit an error instead of a warning, breaking the build.

Alternatives

Unresolved questions

Design meetings