Mono.Cecil-如何获取自定义属性 [英] Mono.Cecil - How to get custom attributes

查看:260
本文介绍了Mono.Cecil-如何获取自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cecil检查与给定方法关联的属性.似乎找到了它,但是我无法使用以下代码获得它的名称:

I am trying to use Cecil to inspect the attributes associated with a given method. It seems to find it, but I cannot get its name using the following code:

AssemblyDefinition assembly = AssemblyFactory.GetAssembly(pathBin);
assembly.MainModule.Types[0].Methods[1].CustomAttributes[0].ToString()

我知道这必须是我将函数设置为的属性,因为当我从dll中删除它时,第二行代码将变为null.我想做的就是能够获取属性的名称.当前,第二行代码将仅返回"Mono.Cecil.CustomAttribute".我猜应该有一种获取属性名称(类类型)名称的方法,对吧?

I know this must be the attribute I've set my function to, because when I remove it from the dll, the second line of code will turn out to null. What I'd like to do is be able to get the attribute's name. Currently the second line of code will return just a "Mono.Cecil.CustomAttribute". I'd guess there should be a way of getting an attribute's name(class type) name, right?

谢谢!

推荐答案

在编写 MoMA .这是它使用的代码:

I had trouble with this when writing MoMA as well. Here is the code it uses:

AssemblyDefinition assembly = AssemblyFactory.GetAssembly(pathBin);
assembly.MainModule.Types[0].Methods[1].CustomAttributes[0].Constructor.DeclaringType.ToString()

这篇关于Mono.Cecil-如何获取自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆