繁体   English   中英

Mono.Cecil - 如何获取自定义属性

[英]Mono.Cecil - How to get custom attributes

我试图使用Cecil检查与给定方法相关的属性。 它似乎找到了它,但我无法使用以下代码获取其名称:

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

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

谢谢!

在编写MoMA时我也遇到了麻烦。 这是它使用的代码:

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

CustomAttributeSystem.Attribute派生类型的实例,因此ToString()将执行作者决定的任何操作。

如果您想了解属性类型,您应该询问它们的类型:

typeInfo.GetCustomAttributes(false)[0].GetType().ToString() ;

我没有看到你正在使用的这个属性CustomAttributes ,所以我宁愿使用我总是使用的MemberInfo.GetCustomAttributes(bool)方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM