我使用的是 Mono.Cecil 0.9.5.3,在安装 VS2012 RC(这会导致 .NET 4.0 System.XML.DLL 被其 .NET 4.5 对应项替换)之后,我在一些系统中遇到了 System.ArugmentException迭代每个方法的自定义属性的代码。原因似乎是在某些情况下,AsyncStateMachine
属性的 ctor 参数(应该是 Type)为空。
下面的一段代码重现了它:
string path = Assembly.Load("System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089").Location;
AssemblyDefinition systemXmlAssembly = AssemblyDefinition.ReadAssembly(path);
var query =
from ModuleDefinition module in systemXmlAssembly.Modules
from TypeDefinition td in module.Types
from MethodDefinition method in td.Methods
from CustomAttribute att in method.CustomAttributes
where method.Name == "System.Xml.IDtdParser.ParseInternalDtdAsync" &&
att.AttributeType.Name == "AsyncStateMachineAttribute"
select att;
CustomAttribute attribute = query.Single();
var args = attribute.ConstructorArguments; // <---- this line throws an ArgumentException
异常是从
抛出的Mono.Cecil.ModuleDefinition.CheckFullName(string fullName = "")
我的问题是 - 这是 Mono.Cecil 或 System.Xml.DLL 中的错误吗?规范是否允许“空”类型作为 ctor 参数出现?
最佳答案
在我看来,这像是 Cecil 中的一个错误,从某种意义上说,Cecil 应该可以读取它而不会崩溃。
您可以在 https://github.com/jbevain/cecil 提交错误吗?并将 4.5 System.XML.dll 上传到某处?然后我会查看,并报告它是实际的 Cecil 问题,还是 System.XML 中编码错误的自定义属性。
更新:
这确实是 Cecil 的问题。现在已经在 master 中修复了。在发布新的 nuget 包之前,您必须自己构建 Cecil。谢谢!
关于c# - Mono.Cecil Exception 在分析.NET 4.5 版本的System.Xml DLL 时抛出,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10968929/