C# 具有开放泛型的System.ComponentModel.Composition MEF
我已经阅读了很多关于MEF和开放通用导出的信息 MEF有两个版本 一个与框架System.ComponentModel.Composition打包* NuGet提供MEF v2,这是一个便携式版本 到目前为止,我已经成功地将MEF用于许多可扩展应用程序,但这是我第一次尝试导出开放泛型C# 具有开放泛型的System.ComponentModel.Composition MEF,c#,.net,mef,C#,.net,Mef,我已经阅读了很多关于MEF和开放通用导出的信息 MEF有两个版本 一个与框架System.ComponentModel.Composition打包* NuGet提供MEF v2,这是一个便携式版本 到目前为止,我已经成功地将MEF用于许多可扩展应用程序,但这是我第一次尝试导出开放泛型 [InheritedExport(typeof(IRepository<>))] public interface IRepository<T> : IRepositoryBase, IDi
[InheritedExport(typeof(IRepository<>))]
public interface IRepository<T> : IRepositoryBase, IDisposable where T : class, IModel, new()
{
T ById(string id);
}
[PartCreationPolicy(CreationPolicy.NonShared)]
[InheritedExport(typeof(IRepository<>))]
public class MyRepository<T> : IRepository<T> where T : class, IModel, new()
{
// concrete imp............
}
那么我想:
var someRepo = _container.GetInstance<IRepository<SomeModel>>();
这部分是出口的,这让我认为这应该是可能的。
不确定是否需要执行任何显式映射,因为我正在使用InheritedExport装饰接口
我正在使用.NET4.5.1和MEF的ComponentModel.Composition版本
开放泛型功能只是NuGet版本MEF的一部分吗
我看到过一些老帖子,上面有人使用MEF contrib和预发布的MEF版本。。但现在这似乎已经过时了
欢迎您提供任何建议。谢谢。如果有人遇到此问题,这只是从组合容器获取实例时的问题。从集成单元测试来看,在进入ImportingConstructor时,OpenGenerics导入似乎确实起作用。但是,我不知道您从哪里获得_container.GetInstance。我只知道。另一件事:MEF的.net-4.0“V1”中的MEF不支持自动关闭打开的类型,您至少需要.net-4.5。