我有这些接口
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。
[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);
}知道怎么让这件事起作用吗?
发布于 2013-12-04 09:52:24
这是一些样品
这些不起作用
基数不匹配导致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")]发布于 2013-12-04 09:34:56
导出和导入类型不匹配。试一试
[Export(typeof(IImageFilter<object,object>))]
public class AnotherFilter1 : IImageFilter<Image<Bgr, byte>, Image<Bgr, byte>>
{
// Implementation...
}https://stackoverflow.com/questions/20370882
复制相似问题