你如何使用ExportFactory< T> [英] How do you use ExportFactory<T>
问题描述
我是新来MEF和ExportFactory试验。我可以使用ExportFactory创建一个基于对象的用户插入列表?样品将类似于如下所示的东西。我可能不理解使用ExportFactory,因为在运行时我得到的组成中如下所示的错误。
1)未找到有效的出口匹配约束
'((exportDefinition.ContractName ==
的System.ComponentModel.Composition.ExportFactory(CommonLibrary.IFoo))
AndAlso运算(exportDefinition.Metadata.ContainsKey(ExportTypeIdentity)
AndAlso运算
System.ComponentModel.Composition.ExportFactory(CommonLibrary。的IFoo)。等于(exportDefinition.Metadata.get_Item(ExportTypeIdentity)))),
无效出口可能已被拒绝。
块引用>
类节目
{
静态无效的主要(字串[] args)
{
测试mytest的=新的测试() ;
}
}
公共类测试:IPartImportsSatisfiedNotification
{
[导入]
私人ExportFactory<的IFoo> FooFactory {搞定;组; }
公开测试()
{
CompositionInitializer.SatisfyImports(本);
CreateComponent(AMP);
CreateComponent(被动);
}
公共无效OnImportsSatisfied()
{
INT I = 0;
}
公共无效CreateComponent(字符串名称)
{
VAR componentExport = FooFactory.CreateExport();
无功补偿= componentExport.Value;
}
}
公共接口IFoo的
{
双名称{;组; }
}
[ExportMetadata(CompType,Foo1)]
[导出(typeof运算(的IFoo))]
[PartCreationPolicy(System.ComponentModel。 Composition.CreationPolicy.NonShared)]
公共类Foo1:IFoo的
{
公共双重名称{;组; }
公共Foo1()
{
}
}
[ExportMetadata(CompType,foo2的)]
[导出(typeof运算(的IFoo))]
[PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)
公共类foo2的:IFoo的
{
公共双名{搞定;组; }
公共foo2的()
{
}
}
解决方案这个问题似乎是,你希望导入一个
ExportFactory<的IFoo>
,但您导出两个不同的的IFoo
实现。在你的榜样,MEF将无法实现两者之间作出选择。
您可能要导入多个工厂,包括像这样的元数据:
[ImportMany]
私人的IEnumerable< ExportFactory<的IFoo,IFooMeta>> FooFactories
{
获得;
组;
}
其中,
IFooMeta code>会声明如下:
公共接口IFooMeta
{
串CompType {搞定; }
}
然后你可以实现
CreateComponent
是这样的:公共IFoo的CreateComponent(字符串名称,字符串compType)
{
VAR matchingFactory = FooFactories.FirstOrDefault(
X => x.Metadata.CompType == compType);
如果(matchingFactory == NULL)
{
抛出新的ArgumentException(
的String.Format('{0}是未知compType,compType),
compType);
}
,否则
{
的IFoo富= matchingFactory.CreateExport()值。
foo.Name =名称;
返回foo的;
}
}
I am new to MEF and experimenting with ExportFactory. Can I use ExportFactory to create a list based on user insertion of objects? A sample would be something similar to what is shown below. I am probably not understanding the use of ExportFactory because during runtime I get an error shown below during composition.
1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "System.ComponentModel.Composition.ExportFactory(CommonLibrary.IFoo)") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "System.ComponentModel.Composition.ExportFactory(CommonLibrary.IFoo)".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.
class Program { static void Main(string[] args) { Test mytest = new Test(); } } public class Test : IPartImportsSatisfiedNotification { [Import] private ExportFactory<IFoo> FooFactory { get; set; } public Test() { CompositionInitializer.SatisfyImports(this); CreateComponent("Amp"); CreateComponent("Passive"); } public void OnImportsSatisfied() { int i = 0; } public void CreateComponent(string name) { var componentExport = FooFactory.CreateExport(); var comp = componentExport.Value; } } public interface IFoo { double Name { get; set; } } [ExportMetadata("CompType", "Foo1")] [Export(typeof(IFoo))] [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)] public class Foo1 : IFoo { public double Name { get; set; } public Foo1() { } } [ExportMetadata("CompType", "Foo2")] [Export(typeof(IFoo))] [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared)] public class Foo2 : IFoo { public double Name { get; set; } public Foo2() { } }
解决方案The problem appears to be that you expect to import a single
ExportFactory<IFoo>
, but you have exported two differentIFoo
implementations. In your example, MEF wouldn't be able to decide between both implementations.You probably want to import multiple factories including metadata like this:
[ImportMany] private IEnumerable<ExportFactory<IFoo,IFooMeta>> FooFactories { get; set; }
where
IFooMeta
would be declared like this:public interface IFooMeta { string CompType { get; } }
and then you could implement
CreateComponent
like this:public IFoo CreateComponent(string name, string compType) { var matchingFactory = FooFactories.FirstOrDefault( x => x.Metadata.CompType == compType); if (matchingFactory == null) { throw new ArgumentException( string.Format("'{0}' is not a known compType", compType), "compType"); } else { IFoo foo = matchingFactory.CreateExport().Value; foo.Name = name; return foo; } }
这篇关于你如何使用ExportFactory< T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!