gpt4 book ai didi

c# - 具有开放通用类型的 MEF

转载 作者:行者123 更新时间:2023-11-30 17:02:02 25 4
gpt4 key购买 nike

我有这些接口(interface)

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);
}

基于这些接口(interface)我创建了类

[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 .

我的建议:

  • 考虑一个非通用的基础接口(interface)
  • 将过滤器导入 IEnumerable 类型的属性,但使用契约(Contract)名称字符串进行导出和导入定义。例如[Export("IMAGE_FILTERS")]

关于c# - 具有开放通用类型的 MEF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20370882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com