簡體   English   中英

具有開放通用類型的MEF

[英]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

我的建議:

  • 考慮一個非通用的基本接口
  • 將過濾器導入IEnumerable類型的屬性,但使用合同名稱字符串來導出和導入定義。 例如[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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM