簡體   English   中英

基於MEF的插件系統無法實例化我的插件

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

我已經C# with MEF實現了一個基於C# with MEF的非常小的插件系統。 問題是,沒有我的插件被實例化。 Aggregate-Catalog我可以看到my plugin listed 但是,在組成這些部分之后,插件列表中沒有我的插件,我在做什么錯呢?

這是我的代碼片段:

插件加載器:

    [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命名空間中的代碼編譯類庫,並將其放置在控制台程序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