Access to the value of a Custom Attribute

前端 未结 7 2331
夕颜
夕颜 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:33

    Cast the object to MethodTestingAttibute:

    object actual = method.Invoke(obj, null);
    
    MethodTestingAttibute attribute = (MethodTestingAttibute)method.GetCustomAttributes(typeof(MethodTestAttribute), true)[0];
    string expected = attribute.Value;
    
    bool areEqual = string.Equals(expected, actual != null ? actual.ToString() : null, StringComparison.Ordinal);
    

提交回复
热议问题