[英]MEF with open generic type
我有这些接口
public interface IImageSource<T>
{
// Properties
T OutputFrame { get; set; }
string Name { get; set; }
Guid Guid { get; set; }
// Events
event EventHandler<DmEventArgs<T>> NewFrameOutput;
// Methods
bool Start();
bool Stop();
void Initialize();
}
public interface IImageFilter<TIn, TOut>
{
// Properties
TIn Input { get; set; }
string Name { get; set; }
Guid Guid { get; set; }
TOut Process(TIn frame);
}
基于这些接口我创建了类
[Export(typeof(IImageSource<>))]
public class AForgeImageSource : IImageSource<Image<Bgr, byte>>
{
// Implementation...
}
[Export(typeof(IImageFilter<,>))]
public class AnotherFilter1 : IImageFilter<Image<Bgr, byte>, Image<Bgr, byte>>
{
// Implementation...
}
问题是如何使用MEF将实现IImageSource的所有对象填充到可观察的集合ImageSources中? 或者如何使用MEF将实现IImageFilter的所有对象都放入可观察的集合ImageFilters中?
我尝试了以下但没有工作。 顺便说一下,我正在使用MEF和MEF Contrib。
[ImportMany]
public ObservableCollection<IImageSource<object>> ImageSources
{
// Implementation...
}
[ImportMany(typeof(IImageFilter<object, object>))]
public ObservableCollection<IImageFilter<object, object>> ImageFilters
{
// Implementation...
}
ImageFilters的集合可以组成
IImageFilter<string, string>
IImageFilter<string, int>
IImageFilter<int, double>
这是我得到MEF来完成作文的代码。
public void LoadPluginList()
{
var pluginsDirectoryPath = Environment.CurrentDirectory + "\\Plugins";
//var aggregateCatalog = new AggregateCatalog();
//var genericCatalog = new GenericCatalog();
var catalog = new DirectoryCatalog(pluginsDirectoryPath);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
知道如何让这个工作吗?
这是一些样本
基数错误导致TestModule
被拒绝。
1。
[Export(typeof(IEnumerable<>))]
public class IntegerCollection: List<int>
{
}
[ModuleExport(typeof(TestModule))]
public class TestModule : Microsoft.Practices.Prism.Modularity.IModule
{
[Import(typeof(IEnumerable<>))]
public IEnumerable<int> Integers
{
get { return m_Integers; }
set { m_Integers = value; }
} private IEnumerable<int> m_Integers;
public void Initialize()
{
}
}
2。
[Export(typeof(IEnumerable<>))]
public class IntegerCollection: List<int>
{
}
[ModuleExport(typeof(TestModule))]
public class TestModule : Microsoft.Practices.Prism.Modularity.IModule
{
[Import]
public IEnumerable<int> Integers
{
get { return m_Integers; }
set { m_Integers = value; }
} private IEnumerable<int> m_Integers;
public void Initialize()
{
}
}
[Export(typeof(IEnumerable<object>))]
public class IntegerCollection: List<int>
{
}
[ModuleExport(typeof(TestModule))]
public class TestModule : Microsoft.Practices.Prism.Modularity.IModule
{
[Import(typeof(IEnumerable<object>))]
public IEnumerable<int> Integers
{
get { return m_Integers; }
set { m_Integers = value; }
} private IEnumerable<int> m_Integers;
public void Initialize()
{
}
}
为什么? 好吧, int
可以从 object
赋值,所以我可以将IEnumerable<int>
导出为IEnumerable<object>
而没有任何问题。
然而,如果我导出一个IEnumerable<double>
和IEnumerable<object>
相反 ,它会失败,因为double
不可分配到int
。
我的建议:
[Export("IMAGE_FILTERS")]
导出和导入类型不匹配。 尝试
[Export(typeof(IImageFilter<object,object>))]
public class AnotherFilter1 : IImageFilter<Image<Bgr, byte>, Image<Bgr, byte>>
{
// Implementation...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.