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

后端 未结 3 1459
遇见更好的自我
遇见更好的自我 2020-11-30 10:11

I have a custom attribute, inside the constructor of my custom attribute I want to set the value of a property of my attribute to the type of the property my attribute was a

3条回答
  •  广开言路
    2020-11-30 10:44

    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"
        }
    }
    

提交回复
热议问题