C# 基于MEF的插件系统可以';不要引用我的插件

C# 基于MEF的插件系统可以';不要引用我的插件,c#,plugins,reflection,mef,C#,Plugins,Reflection,Mef,我已经实现了一个非常小的插件系统,它基于MEFC。问题是,我的插件都没有实例。在聚合目录中我可以看到我的插件列表。但是,在我编写这些部分之后,插件列表中没有我的插件,我做错了什么 以下是我的代码片段: 插件加载程序: [ImportMany(typeof(IFetchService))] private IFetchService[] _pluginList; private AggregateCatalog _pluginCatalog; private con

我已经实现了一个非常小的插件系统,它基于MEF
C。问题是,我的插件都没有实例。在
聚合目录中
我可以看到
我的插件列表
。但是,在我编写这些部分之后,插件列表中没有我的插件,我做错了什么

以下是我的代码片段:

插件加载程序:

    [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);
     }
     ...
在这里,插件本身:

[Export(typeof(IFetchService))]
public class MySamplePlugin : IFetchService
{
    public MySamplePlugin()
    {
        Console.WriteLine("Plugin entered");
    }
    ...
}
测试工作样本

使用PluginNameSpace命名空间中的代码编译类库,并将其放置到控制台app 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");
        }
    }
}

我在控制台应用程序中复制了你的代码,它工作正常。你读过我的帖子吗?在这篇文章中,我已经说过,我的插件列在AggregateCatalog中。只有ComposeContainer无法组合这些部分。我的插件是一个独立的库。我自己解决了这个问题。接口IFetchService应该由自己的库分开。该库应从两侧引用。现在,我的插件已加载并实例化。