AppDomain.CurrentDomain.GetAssemblies 因 ReflectionTypeLoadException 失败 [英] AppDomain.CurrentDomain.GetAssemblies fails with ReflectionTypeLoadException
问题描述
在单元测试期间,我遇到了以下代码的问题,该代码要求所有加载的程序集:
During unittesting I have run into a problem with the following code that asks for all the loaded assemblies:
var res = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(x => x.GetTypes())
.ToList();
此代码因 ReflectionTypeLoadException 失败,该异常具有模式的内部异常
this code fails with a ReflectionTypeLoadException which has inner exceptions of the pattern
无法加载类型 Microsoft.Xml.Serialization.GeneratedAssembly.FOO
Could not load type Microsoft.Xml.Serialization.GeneratedAssembly.FOO
其中 FOO
是我们也编写的一些特定类.
where FOO
are some specific classes also coded by us.
在使用 XDocument 类创建 XML 文档之前运行单元测试时会出现问题.
The problem arises when running unittests prior to the above which creates XML documents using the XDocument class.
我可能并不一定要加载这些代码生成的类(我猜是 Microsoft.Xml.Serialization.GeneratedAssembly.* 是代码生成的.)我只是想了解有什么问题.
I may not necesarilly want to load these code generated classes (I'm guessing Microsoft.Xml.Serialization.GeneratedAssembly.* is code generated.) I just want to understand whats wrong.
推荐答案
您应该检查程序集的 IsDynamic 字段,该字段可在 .netframework 4 及更高版本中使用.
You should check IsDynamic field of your assembly which is availabe at .netframework 4 and later.
var res = AppDomain.CurrentDomain.GetAssemblies().Where(ass => ass.IsDynamic == false)
.SelectMany(x => x.GetTypes())
.ToList();
这篇关于AppDomain.CurrentDomain.GetAssemblies 因 ReflectionTypeLoadException 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!