简体   繁体   English

基于MEF的插件系统无法实例化我的插件

[英]MEF based plugin system can't instance my plugins

I've implemented a very small plugin system based on C# with MEF . 我已经C# with MEF实现了一个基于C# with MEF的非常小的插件系统。 The problem is, none of my plugins are instanced. 问题是,没有我的插件被实例化。 In the Aggregate-Catalog I can see my plugin listed . Aggregate-Catalog我可以看到my plugin listed But, after I'll compose these parts, there isn't my plugin in the plugin list, what I'm doing wrong? 但是,在组成这些部分之后,插件列表中没有我的插件,我在做什么错呢?

Here's a snippet of my code: 这是我的代码片段:

Plugin-Loader: 插件加载器:

    [ImportMany(typeof(IFetchService))]
    private IFetchService[] _pluginList;
    private AggregateCatalog _pluginCatalog;
    private const string pluginPathKey = "PluginPath";
    ...

    public PluginManager(ApplicationContext context)
    {
        var dirCatalog = new DirectoryCatalog(ConfigurationManager.AppSettings[pluginPathKey]);
        //Here's my plugin listed...
        _pluginCatalog = new AggregateCatalog(dirCatalog);

        var compositionContainer = new CompositionContainer(_pluginCatalog);
        compositionContainer.ComposeParts(this);
     }
     ...

And here, the plugin itself: 在这里,插件本身:

[Export(typeof(IFetchService))]
public class MySamplePlugin : IFetchService
{
    public MySamplePlugin()
    {
        Console.WriteLine("Plugin entered");
    }
    ...
}

Tested working sample. 经过测试的工作样品。

Compile class library with code inside PluginNameSpace namespace and place it to the 'Test' folder which will be inside console app exe folder. 使用PluginNameSpace命名空间中的代码编译类库,并将其放置在控制台程序exe文件夹中的“ Test”文件夹中。

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Reflection;
using ConsoleApplication;

namespace ConsoleApplication
{
    public interface IFetchService
    {
        void Write();
    }

    class PluginManager
    {
        [ImportMany(typeof(IFetchService))]
        public  IFetchService[] PluginList;

        public PluginManager()
        {
            var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Test");

            var pluginCatalog = new AggregateCatalog(dirCatalog);
            var compositionContainer = new CompositionContainer(pluginCatalog);
            compositionContainer.ComposeParts(this);
         } 
    }

    class Program
    {
        static void Main(string[] args)
        {
            var pluginManager = new PluginManager();

            foreach (var fetchService in pluginManager.PluginList)
            {
                fetchService.Write();
            }

            Console.ReadKey();
        }
    }
}

// Separate class library
namespace PluginNameSpace
{
    [Export(typeof(IFetchService))]
    public class MySamplePlugin : IFetchService
    {
        public void Write()
        {
            Console.WriteLine("Plugin entered");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM