Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用MEF与事件交互_C#_Winforms_Mef - Fatal编程技术网

C# 使用MEF与事件交互

C# 使用MEF与事件交互,c#,winforms,mef,C#,Winforms,Mef,我最近研究了使用MEF来构建插件框架,也阅读了不少文章和教程,但是我试图实现的(我还没有看到这样的示例)是提供一个访问点,这样我就可以在(例如)表单加载点从设置的目录加载插件,为了改变控制或防止加载ect,另一个例子是点击按钮,以扩大或再次防止标准功能发生。有谁能告诉我其他资源的方向,或者提供一个简单的例子来解释如何实现这一点 TIA这是一个简单的实现示例。首先将参考System.ComponentModel.Composition添加到项目中 声明插件接口: [InheritedExport]

我最近研究了使用MEF来构建插件框架,也阅读了不少文章和教程,但是我试图实现的(我还没有看到这样的示例)是提供一个访问点,这样我就可以在(例如)表单加载点从设置的目录加载插件,为了改变控制或防止加载ect,另一个例子是点击按钮,以扩大或再次防止标准功能发生。有谁能告诉我其他资源的方向,或者提供一个简单的例子来解释如何实现这一点


TIA

这是一个简单的实现示例。首先将参考
System.ComponentModel.Composition
添加到项目中

声明插件接口:

[InheritedExport]
public interface IPlugin
{
    string Name { get; }
}
在同一个或另一个组件中,实现以下接口

public class Plugin1 : IPlugin
{
    public string Name
    {
        get { return "Plugin#1"; }
    }
}
稍后使用
DirectoryCatalog
AssemblyCatalog
构建目录

public class CatalogManager : IDisposable
{
    [ImportMany(typeof (IPlugin), AllowRecomposition = true)] 
    private IEnumerable<IPlugin> _plugins;

    private CompositionContainer _container;

    public CompositionContainer Container
    {
        get { return _container; }
    }

    public IEnumerable<IPlugin> Plugins
    {
        set { _plugins = value; }
    }

    private CatalogManager(string pluginsDir)
    {
        var catalog = new AggregateCatalog();

        //--load all plugin from plugin directory
        if (Directory.Exists(pluginsDir))
            catalog.Catalogs.Add(new DirectoryCatalog(pluginsDir));

        //--load all plugin from executing assembly
        catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));

        Initialize(catalog);
    }

    private void Initialize(AggregateCatalog catalog)
    {
        _container = new CompositionContainer(catalog);

        _container.ComposeParts(this);
    }

    public void Dispose()
    {
        if (_container == null)
            return;

        _container.Dispose();
        _container = null;
    }
}
公共类目录管理器:IDisposable
{
[ImportMany(typeof(IPlugin),AllowRecomposition=true)]
私有IEnumerable插件;
私有合成容器_容器;
公共合成容器
{
获取{return\u container;}
}
公共IEnumerable插件
{
设置{u plugins=value;}
}
私有目录管理器(字符串插件目录)
{
var catalog=new AggregateCatalog();
//--从插件目录加载所有插件
if(Directory.Exists(pluginsDir))
catalog.Catalogs.Add(新目录目录(pluginsDir));
//--从正在执行的程序集中加载所有插件
catalog.Catalogs.Add(新的AssemblyCatalog(Assembly.getExecutionGassembly());
初始化(目录);
}
私有void初始化(AggregateCatalog目录)
{
_容器=新的合成容器(目录);
_容器。组件(本);
}
公共空间处置()
{
if(_container==null)
返回;
_container.Dispose();
_container=null;
}
}

这个答案是一个很好的开始。但我不知道它在哪里处理事件。