微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – PostSharp:使用OnMethodInvocationAspect时,将删除自定义属性

我有一些这样的方面:
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

然后我可以调用我的属性的get_方法获取我想要的数据:

foreach (PropertyInfo pi in typeof(T).GetProperties())
        {
            var entityAttribute = (MyAttribute)(typeof(T).getmethod("get_" + pi.Name).GetCustomAttributes(typeof(MyAttribute),true).FirstOrDefault());
        }

原文地址:https://www.jb51.cc/csharp/97371.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐