我有一些这样的方面:
public class MyAttribute : OnMethodInvocationAspect { public int Offset { get; internal set; } public MyAttribute(int offset) { this.Offset = offset; } public override void OnInvocation(MethodInvocationEventArgs eventArgs) { //do some stuff } }
class MyClass { [MyAttribute(0x10)] public int MyProp { get; set; } }
工程一切正常然而现在我想用反射来得到我的补偿;当我做的
typeof(MyClass).GetProperty("MyProp").GetCustomAttributes(true);
解决方法
啊,我这样修复了:
[MulticastAttributeUsage(MulticastTargets.Method,PersistMetaData=true)] public class MyAttribute : OnMethodInvocationAspect
foreach (PropertyInfo pi in typeof(T).GetProperties()) { var entityAttribute = (MyAttribute)(typeof(T).GetMethod("get_" + pi.Name).GetCustomAttributes(typeof(MyAttribute),true).FirstOrDefault()); }