c#插件扩展目录(MEF) [英] c# Plugin Extension Directory (MEF)

查看:190
本文介绍了c#插件扩展目录(MEF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MEF体系结构.

I have a mef architecture.

访问我的扩展插件目录的正确方法是什么.

What is the correct way to approach my extension plugin directory.

1.我是否要创建一个单独的目录调用扩展"?插件?

1. Do I create a separate directory call "Extensions" for plugin?

2.如果是这样,在我的项目中链接或构建所有插件程序集的可靠方式是什么?在何处?如何实现?

2. If so, where, how, and what is the robust way about linking or building all plugin assemblies in my project?

我正在尝试找到扩展插件目录的可靠方法吗?

I am trying to find the robust way to go about the extension plugin directory?

pianoboyCoder

pianoboyCoder

推荐答案

boyboyCoder,

感谢您在此处发布.

对于您的问题,您可以参考到 链接 .

For your question, you could refer to the link.

> ;> 1.我是否要创建一个单独的目录调用扩展"?插件?

您可以执行以下操作呼叫分机"插件.

namespace Host {

    class MainClass {

        private static PluginHost plugins;

        public static void Main(string[] args) {
            plugins = new PluginHost();
            foreach(IPlugIn plugin in plugins.plugins) {
                plugin.Execute();
            }
        }
    }


    class PluginHost{

        [ImportMany(typeof(IPlugIn))]
        public List<IPlugIn> plugins = new List<IPlugIn>();

        public PluginHost() {
            var catalog = new DirectoryCatalog("PlugIns");
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);   
        }   
    }
}


> ;> 2.如果是这样,在我的项目中链接或构建所有插件程序集的可靠方式是什么?在哪里,如何做?有什么健壮的方法?

为此,您可以尝试通过使用System.Reflection.Assembly.LoadFile( …加载插件文件来手动替换DirectoryCatalog. … ); 然后使用 AssemblyCatalogs 加载所有找到的程序集:

For this, you could try to replace the DirectoryCatalog manually by loading the PlugIn-files with System.Reflection.Assembly.LoadFile(……); and then using AssemblyCatalogs to load all found assemblies:

class PluginHost{

    [ImportMany(typeof(IPlugIn))]
    public List<IPlugIn> plugins = new List<IPlugIn>();

    public PluginHost() {
        List<AssemblyCatalog> catalogs = new List<AssemblyCatalog>();
        foreach(string plfile in Directory.GetFiles("PlugIns", "*.dll", SearchOption.AllDirectories)) {
            catalogs.Add(new AssemblyCatalog(System.Reflection.Assembly.LoadFile(plfile)));
        }           
        var container = new CompositionContainer(new AggregateCatalog(catalogs));
        container.ComposeParts(this);   
    }   
}

此处是有关的更多信息 使用MEF在C#中导入多个扩展程序集 以供参考.

Here is more information about Importing multiple extension assemblies in C# with MEF for reference.


希望对您有所帮助.


I hope this would be helpful to you.

最好的问候,

Wendy

Wendy


这篇关于c#插件扩展目录(MEF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆