能否MEF的应用程序建立在构建在.NET 3.5 .NET 4的进口类型?应用程序、类型、MEF、NET

2023-09-07 08:45:09 作者:骚年称霸帝王座

我正在开发使用托管扩展框架主机应用程序,它的建成对.NET 4中,并且内置于框架System.ComponentModel.Composition组装。我想支持发展的部分使用.NET 3.5和声明导出它们的能力。

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.

由于出口属性是新的.NET 4中,因此不能由.NET 3.5组件被引用,我不知道去了解出口地区的最佳方式。有一个简单的方法来做到这一点没有实现,它使用一些其他的机制来发现出口的新型目录?

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?

推荐答案

您可以编写一个处理程序的 AppDomain.AssemblyResolve 当3.5版本要求,将返回的.NET版本4 MEF的。但是,你写的处理程序将只使用,如果实在找不到使用默认绑定逻辑组件,所以你需要确保3.5 MEF DLL不可用,或者可能加载扩展组件,没有上下文( 看到这里),从解决该DLL prevent它。

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.