我已经阅读了很多关于 MEF 和Open generic exports的信息。
有两种版本的 MEF ..
到目前为止,我已成功将MEF用于多个可扩展应用程序,但这是我第一次尝试导出开放式泛型。
[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
修饰接口)。
我正在使用.NET 4.5.1和MEF的ComponentModel.Composition版本。
开放通用功能只是NuFet版MEF的一部分吗?
我看过人们使用MEF contrib和预先发布的MEF版本的旧帖子..但现在看来已经老了
欢迎任何建议。谢谢。