4

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

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

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

谢谢!

4

2 回答 2

7

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

AssemblyDefinition assembly = AssemblyFactory.GetAssembly(pathBin);
assembly.MainModule.Types[0].Methods[1].CustomAttributes[0].Constructor.DeclaringType.ToString()
于 2009-08-06T14:24:32.113 回答
-3

A是派生类型CustomAttribute的一个实例,所以无论作者决定什么都会做。System.AttributeToString()

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

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

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

于 2009-08-06T14:54:08.337 回答