C# MEF存储库导出<;T>;:IRepository<;T>;
我正在尝试使用MEF导出以下内容:C# MEF存储库导出<;T>;:IRepository<;T>;,c#,generics,mef,C#,Generics,Mef,我正在尝试使用MEF导出以下内容: [Export(typeof(IRepository<>))] public class Repository<T> : IRepository<T> where T : class { [Export(typeof(IRepository))] 公共类存储库:IRepository T:在哪里上课 { 带着进口的 [Import(typeof(IRepository<>))] private IRe
[Export(typeof(IRepository<>))]
public class Repository<T> : IRepository<T>
where T : class
{
[Export(typeof(IRepository))]
公共类存储库:IRepository
T:在哪里上课
{
带着进口的
[Import(typeof(IRepository<>))]
private IRepository<Contact> repository;
[Import(typeof(IRepository))]
私有存储库;
但我在编写以下MEF时不断收到错误消息:
=========================================
合成保持不变。由于以下错误,更改被拒绝:合成产生了一个合成错误。下面提供了根本原因。有关详细信息,请查看CompositionException.Errors属性
1) 找不到与约束“((exportDefinition.ContractName=“Interfaces.IRepository()”&(exportDefinition.Metadata.ContainsKey(“ExportTypeIdentity”)&&“Interfaces.IRepository()”.Equals(exportDefinition.Metadata.get_Item(“ExportTypeIdentity”))匹配的有效导出,无效导出可能已被拒绝
导致:无法在部分“SoCLINQ2SQL.RepositoryTest”上设置导入“SoCLINQ2SQL.RepositoryTest.repository(ContractName=“Interfaces.IRepository()”)。
元素:SoCLINQ2SQL.RepositoryTest.repository(ContractName=“Interfaces.IRepository()”-->SoCLINQ2SQL.RepositoryTest,据我所知,根据主题,MEF不支持“开箱即用”的开放泛型类型 很明显,这一点在美国得到了支持 我相信在这种情况下,您可以将导出作为开放的泛型类型,但在导入方面,您需要将导入更改为:
[Import(typeof(IRepository<Contact>))]
private IRepository<Contact> repository;
[Import(typeof(IRepository))]
私有存储库;
我有一个类似的问题,它与程序集添加到AggregateCatalog的顺序有关。下面的示例演示了引导程序。ConfigureAggregateCatalog()“模块B”正在从“模块C”调用服务,但“模块C”还没有添加到聚合目录中。我只需更改顺序,问题就解决了
protected override void ConfigureAggregateCatalog()
{
base.ConfigureAggregateCatalog();
// Be aware of the order on which the assemblies are added to the AggregateCatalog.
// It's important to add the assembly to the AggregateCatalog in the correct order, otherwise
// you may get the error "No valid exports were found that match the constraint".
// In the example below, if Module B invokes a method of Module C, module C must be
// added to the AggregateCatalog prior to Module B.
// Please note the Bootstrapper assembly also needs to be added to the AggregateCatalog.
// --------------------------------------------------------------------------------------
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleA).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleC).Assembly));
this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(ModuleB).Assembly));
}
仅供参考:这是支持在即将到来的版本。预览下降应该在我们的codeplex网站很快