MEF合成期间处理ReflectionTypeLoadException [英] Handle ReflectionTypeLoadException during MEF composition
问题描述
我在MEF中使用DirectoryCatalog
来满足我的应用程序中的导入要求.但是,有时在目录中存在混淆的程序集,当我尝试编写目录时会导致ReflectionTypeLoadException
.
I am using a DirectoryCatalog
in MEF to satisfy imports in my application. However, there are sometimes obfuscated assemblies in the directory that cause a ReflectionTypeLoadException
when I try to compose the catalog.
我知道我可以通过使用单独的目录或在DirectoryCatalog
上使用搜索过滤器来解决此问题,但我想采用更通用的方法来解决此问题.有什么方法可以处理异常并允许合成继续进行?还是还有其他更通用的解决方案?
I know I can get round it by using a separate directory or by using a search filter on the DirectoryCatalog
but I want a more general way to solve the problem. Is there some way I can handle the exception and allow composition to continue? Or is there another more general solution?
推荐答案
DirectoryCatalog
已经具有捕获ReflectionTypeLoadException
并忽略这些程序集的代码.不幸的是,由于我已经已报告,仅创建AssemblyCatalog
仍不会触发异常,因此代码不起作用.
DirectoryCatalog
already has code to catch ReflectionTypeLoadException
and ignore those assemblies. Unfortunately, as I have reported, merely creating the AssemblyCatalog
will not yet trigger the exception so that code doesn't work.
该异常实际上是由第一次调用AssemblyCatalog.Parts
触发的.
The exception is actually triggered by the first call to AssemblyCatalog.Parts
.
您不必自己使用MEF的DirectoryCatalog
,而是必须自己做:
Instead of using the DirectoryCatalog
from MEF, you will have to do it yourself:
- 扫描目录以查找程序集
- 加载每个程序集并为其创建一个
AssemblyCatalog
- 调用
AssemblyCatalog.Parts.ToArray()
强制异常并捕获它 - 用
AggregateCatalog
汇总所有好的目录
- scan a directory for assemblies
- load each assembly and creates a
AssemblyCatalog
for it - invoke
AssemblyCatalog.Parts.ToArray()
to force the exception, and catch it - aggregate all the good catalogs with a
AggregateCatalog
这篇关于MEF合成期间处理ReflectionTypeLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!