0

我想在运行时从我的插件目录加载几个程序集。核心应用程序事先没有对程序集的引用,程序集实现了核心应用程序给定的接口,因此对其具有引用。我发现 2005 年的文章准确地解释了我需要做些什么来完成这项工作

目前我有这段代码,它基本上是您在上面文章中找到的 LINQ 版本:

foreach (
    IModule module in
        Directory.EnumerateDirectories(string.Format(@"{0}/{1}", Application.StartupPath, ModulePath))
            .Select(dir => Directory.GetFiles(dir, "*.dll"))
            .SelectMany(files => files.Select(Assembly.LoadFrom)
                                        .SelectMany(assembly => (from type in assembly.GetTypes()
                                                                where type.IsClass && !type.IsNotPublic
                                                                let interfaces = type.GetInterfaces()
                                                                where
                                                                    ((IList) interfaces).Contains(
                                                                        typeof (IModule))
                                                                select
                                                                    (IModule) Activator.CreateInstance(type))))
    )
{
    module.Core = this;
    module.Initialize();
    AddModule(module);
}

那么当前在运行时动态加载插件/模块/程序集的方法是什么?

4

3 回答 3

3

您可能还想查看Microsoft Managed Extensibility Framework以及本教程,它是一个不错的介绍。

于 2013-04-09T07:24:30.417 回答
0

我建议托管可扩展性框架

或者,如果您需要加载“插件”而无需保留引用,还有PRISM

于 2013-04-09T07:24:31.477 回答
0

roslyn 项目没有提供一些有用的东西吗?

http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx

http://www.developerfusion.com/article/143896/understanding-the-basics-of-roslyn/

var scriptEngine = new ScriptEngine();
var session = Session.Create();
scriptEngine.Execute("foo.dll", session);
于 2013-04-09T07:25:21.457 回答