Can a MEF application built on .NET 4 import types built against .NET 3.5?

依然范特西╮ 提交于 2019-12-12 16:43:46

问题


I am developing a host application using Managed Extensibility Framework and it's built against .NET 4 and the System.ComponentModel.Composition assembly that is built into the framework. I would like to support the ability to develop parts using .NET 3.5 and export them declaratively.

Since the export attributes are new in .NET 4 and thus cannot be referenced by the .NET 3.5 assembly, I'm not sure the best way to go about exporting parts. Is there an easy way to do this without implementing a new type of catalog that uses some other mechanism for discovering exports?


回答1:


You can write a handler for AppDomain.AssemblyResolve that will return the .NET 4 version of MEF when the 3.5 version is requested. However, the handler you write will only be used if it can't find the assembly using the default binding logic, so you would need to make sure the 3.5 MEF DLL wasn't available, or possibly load the extension assemblies with no context (see here) to prevent it from resolving to that DLL.




回答2:


You might be able to use an Assembly Binding Redirect to have the extensions compiled against a .NET 3.5 version of MEF but have them use the .NET 4 version at runtime.



来源:https://stackoverflow.com/questions/3406259/can-a-mef-application-built-on-net-4-import-types-built-against-net-3-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!