在 MEF 组合期间处理 ReflectionTypeLoadException
我在 MEF 中使用 DirectoryCatalog
来满足应用程序中的导入。但是,当我尝试编写目录时,目录中有时会出现混淆的程序集,导致出现 ReflectionTypeLoadException
。
我知道我可以通过使用单独的目录或使用 DirectoryCatalog
上的搜索过滤器来解决这个问题,但我想要一种更通用的方法来解决问题。有什么方法可以处理异常并允许组合继续吗?或者还有其他更通用的解决方案?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了避免其他人编写自己的 SafeDirectoryCatalog 实现,这是我根据 Wim Coenen 的建议提出的实现:
To save others from writing their own implementation of the SafeDirectoryCatalog, here is the one I came up with based upon Wim Coenen's suggestions:
DirectoryCatalog
已经具有捕获ReflectionTypeLoadException
并忽略这些程序集的代码。不幸的是,正如我报告的,仅仅创建AssemblyCatalog
还不能触发异常,使代码不起作用。该异常实际上是由第一次调用
AssemblyCatalog.Parts
触发的。您必须自己执行此操作,而不是使用 MEF 中的
DirectoryCatalog
:AssemblyCatalog 创建一个
强制异常,并捕获它AssemblyCatalog
。 Parts.ToArray()DirectoryCatalog
already has code to catchReflectionTypeLoadException
and ignore those assemblies. Unfortunately, as I have reported, merely creating theAssemblyCatalog
will not yet trigger the exception so that code doesn't work.The exception is actually triggered by the first call to
AssemblyCatalog.Parts
.Instead of using the
DirectoryCatalog
from MEF, you will have to do it yourself:AssemblyCatalog
for itAssemblyCatalog.Parts.ToArray()
to force the exception, and catch itAggregateCatalog
我是通过我正在编写的 API 执行此操作的,并且 SafeDirectoryCatalog 不会记录与来自不同程序集的单个导入匹配的多个导出。 MEF 调试通常通过调试器和 TraceListener 完成。我已经使用了 Log4Net,并且我不希望有人需要在配置文件中添加另一个条目只是为了支持日志记录。 http ://blogs.msdn.com/b/dsplaisted/archive/2010/07/13/how-to-debug-and-diagnose-mef-failures.aspx我想出了:
I was doing this from an API I was writing and the SafeDirectoryCatalog would not log multiple exports matching a single import from different assemblies. MEF debugging is typically done via debugger and TraceListener. I already used Log4Net and I didn't want someone to need to add another entry to the config file just to support logging. http://blogs.msdn.com/b/dsplaisted/archive/2010/07/13/how-to-debug-and-diagnose-mef-failures.aspx I came up with: