Access to the value of a Custom Attribute

前端 未结 7 2330
夕颜
夕颜 2021-01-03 20:17

I\'ve got this custom attribute:

[AttributeUsage(AttributeTargets.Method, AllowMultiple=false, Inherited = true)]
class MethodTestingAttibute : Attribute
{           


        
7条回答
  •  我在风中等你
    2021-01-03 20:35

    To get the value of an attribute property, just cast the object returned by GetCustomAttributes():

    {
        string val;
        object[] atts = method.GetCustomAttributes(typeof(MethodTestAttibute), true);
        if (atts.Length > 0)
           val = (atts[0] as MethodTestingAttibute).Value;
    }
    

提交回复
热议问题