问题描述
我有一些进口的部分,我需要创建的多个实例。通过围绕搜索,我决定我需要使用ExportFactory类。不幸的是,ExportFactory类不可用WPF中默认,但幸运的是格伦座具有
I have some part imports that I need to create multiple instances of. By searching around I decided I needed to use the ExportFactory class. Unfortunately, the ExportFactory class is not available in WPF by default, but luckily Glenn Block has ported the code.
本来,我是导入时指定类型:
Originally, I was specifying the type when importing:
[ImportMany(typeof(IMyModule))]
public IEnumerable<Lazy<IMyModule, IMyModuleMetadata>> Modules { get; set; }
我还创建了一个出口属性:
I also created an export attribute:
[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class ExportMyModuleMetadata : ExportAttribute, IMyModuleMetadata
{
public ExportMyModuleMetadata(string category, string name)
: base(typeof(IMyModuleData))
{
Category = category;
Name = name;
}
public string Category { get; set; }
public string Name { get; set; }
}
我的出口是这样的:
My export looks like this:
[ExportMyModuleMetadata("Standard","Post Processor")]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class Module1 : IMyModuleData
以上的进口工作的罚款。但是,一旦我改变了懒< T,T>
到 ExportFactory< T,T>
我开始撰写期间错误
The above import worked fine. But once I changed Lazy<T,T>
to ExportFactory<T,T>
I started getting an error during composition.
[ImportMany(typeof(IMyModule))]
public IEnumerable<ExportFactory<IMyModule, IMyModuleMetadata>> Modules { get; set; }
我得到的错误信息是:
The error message I got was:
The export 'Module1 (ContractName="IMyModule")' is not assignable to type
'System.ComponentModel.Composition.ExportFactory`
我看到的地方(我找不到链接现在)的指定键入
在 ImportMany
属性的问题。我想没有,所以我删除从类型我可以做的 ImportMany
。
I saw somewhere (that I can't find the link for right now) that specifying the Type
in the ImportMany
attribute is the problem. I figured I could do without it so I removed the type from the ImportMany
.
[ImportMany()]
public IEnumerable<Lazy<IMyModule, IMyModuleMetadata>> Modules { get; set; }
使用时,该进口仍然工作懒< T,T>
,但一旦我把它改为 ExportFactory< T,T>
,我不再进口任何东西。我没有得到一个错误了,但没有得到进口的。
This import still worked when using Lazy<T,T>
, but once I changed it to ExportFactory<T,T>
, I no longer import anything. I didn't get an error anymore but nothing gets imported.
有谁知道如何正确使用 ImportMany
与 ExportFactory< T,T>
为WPF
Does anyone know how to properly use ImportMany
with ExportFactory<T,T>
for WPF?
更新:
随着韦斯的关于添加 ExportFactoryProvider()
,我得到了 ExportFactory<尖; T,T>
.NET 4的工作! ,以下是更新的组成码
With Wes's tip about adding an ExportFactoryProvider()
, I got the ExportFactory<T,T>
working in .NET 4! Below is the updated composition code.
var ep = new ExportFactoryProvider();
//Looks for modules in main assembly and scans folder of DLLs for modules.
var moduleCatalog = new AggregateCatalog(
new AssemblyCatalog(runningApp),
new DirectoryCatalog(".", "*.dll"));
var container = new CompositionContainer(moduleCatalog, ep);
ep.SourceProvider = container;
var Modules = new Modules();
container.ComposeParts(Modules);
我也发现了这个讨论在的,讨论多一点这个问题。
I also found a discussion on this at MEF Codeplex site that talks a little more about this.
推荐答案
在一般的.NET 4.0 ExportFactory不支持开箱即用。 ExportFactory是特殊类型的容器(或自定义的出口供应商)知道和特别对待,并根据您收到它看起来并不像这个容器中的错误信息知道ExportFactory什么特别的,因为它试图将其转换为IMyModule 。
In general on .NET 4.0 ExportFactory isn't supported out of the box. ExportFactory is special type that the container (or a custom export provider) knows about and treats specially and based on the error message you have received it doesn't look like this container knows anything special about ExportFactory because it is trying to cast it to IMyModule.
看一看格伦的测试ExportFactory你一个Microsoft.ComponentModel.Composition.Hosting.ExportFactoryProvider添加到您的容器?
Have a look at Glen's tests for ExportFactory did you add a Microsoft.ComponentModel.Composition.Hosting.ExportFactoryProvider to your container?
另外请注意,如果你有一个选项切换到.NET 4.5,ExportFactory支持开箱即用。
Also note that if you have an option to switch to .NET 4.5, ExportFactory is supported out of the box.
这篇关于MEF - [ImportMany]使用ExportFactory< T>在WPF - .NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!