Get member to which attribute was applied from inside attribute constructor?

It’s possible from .NET 4.5 using CallerMemberName:

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

Then your attribute:

[AttributeUsage(AttributeTargets.Property)]
public class SomethingCustomAttribute : Attribute
{
    public StartupArgumentAttribute([CallerMemberName] string propName = null)
    {
        // propName == "MyProperty"
    }
}

Leave a Comment