您需要
GetCustomAttributes在
Methodbase对象上调用该函数。
获取
Methodbase对象的最简单方法是调用
Methodbase.GetCurrentMethod。(请注意,您应该添加
[MethodImpl(MethodImplOptions.NoInlining)])
例如:
Methodbase method = Methodbase.GetCurrentMethod();MyAttribute attr = (MyAttribute)method.GetCustomAttributes(typeof(MyAttribute), true)[0] ;string value = attr.Value; //Assumes that MyAttribute has a property called Value
您也可以
Methodbase手动获取,例如:(这样会更快)
Methodbase method = typeof(MyClass).GetMethod("MyMethod");


