在运行时加载程序集的2013方法是什么?

时间:2013-04-09 07:21:24

标签: c# plugins .net-assembly

我想在运行时从我的插件目录加载几个程序集。核心应用程序事先没有对程序集的引用,程序集实现了核心应用程序给出的接口,因此引用了它。 I found and article from 2005 explaining exactly what I need to do to make this work

目前我有这段代码,它基本上是你在上面文章中找到的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);
}

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

3 个答案:

答案 0 :(得分:3)

你可能想看看Microsoft Managed Extensibility Framework 这个Tutorial这个有点好的介绍。

答案 1 :(得分:0)

我建议Managed Extensibility Framework

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

答案 2 :(得分: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);
相关问题