使用.NET 4中的DirectoryCatalog创建同一部件的多个实例

时间:2015-09-04 14:58:54

标签: c# .net-4.0 mef

我正在尝试创建使用DirectoryCatalog导入的同一部分的多个实例,以便让我已经使用相同部分的“个人”实例,如下所示:

interface TMyType
{
    some method...
}

public class PluginHelper
{
    ImportMany of type TMyType with CreationPolicy non shared
    ...

    public bool GetPluginByName(string name)
    {
        GetPlugin based on name requested
    }
}

class A
{
    plugin = PluginHelper.GetPluginByName("PLUGIN1")
}

class B
{
    plugin = PluginHelper.GetPluginByName("PLUGIN1")
}

这实际上有效,但A.plugin与B.plugin是相同的实例...使用.Net 4(不是4.5)看起来我应该在每个类中移动导入,然后在那里选择所需的插件。我知道我可以使用Lazy以避免在合成中创建部件,但看起来仍然有点难看......

谷歌搜索相当多无济于事。

TIA

2 个答案:

答案 0 :(得分:1)

为什么不直接使用工厂?

$("#grid").kendoGrid({
    dataSource: {
        data: jsondata.rows,
        schema: {
            model: {
                fields: {
                    ProductName: { type: "string" },
                    UnitPrice: { type: "number" },
                    UnitsInStock: { type: "number" },
                    Discontinued: { type: "boolean" }
                }
            }
        },
        group: {
            field: jsondata.groupBy,
            dir: "asc"
        }
    },
    groupable: true,
    scrollable: true,
    columns: [
        "ProductName",
        { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: "130px" },
        { field: "UnitsInStock", title: "Units In Stock", width: "130px" },
        { field: "Discontinued", width: "130px" }
    ]
});

答案 1 :(得分:0)

我认为这是我的问题的解决方案,也是斯蒂芬试图解释我的原因:)

Factory pattern with Managed Ext Framwork (MEF)

相关问题