加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 18:03:02 所属栏目:百科 来源:网络整理
导读:我有一些这样的方面: public class MyAttribute : OnMethodInvocationAspect{ public int Offset { get; internal set; } public MyAttribute(int offset) { this.Offset = offset; } public override void OnInvocation(MethodInvocationEventArgs eventAr
我有一些这样的方面:
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());
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读