【问题标题】:What is the 2013 way to load an assembly at runtime?在运行时加载程序集的 2013 年方式是什么?
【发布时间】:2013-04-09 07:21:24
【问题描述】:

我想在运行时从我的插件目录加载几个程序集。核心应用程序事先没有对程序集的引用,程序集实现了核心应用程序给定的接口,因此对其具有引用。 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);
}

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

【问题讨论】:

标签: c# plugins .net-assembly


【解决方案1】:

您可能想看看Microsoft Managed Extensibility Framework 还有这个Tutorial 它有点不错的介绍。

【讨论】:

    【解决方案2】:

    我建议Managed Extensibility Framework

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

    【讨论】:

      【解决方案3】:

      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);
      

      【讨论】:

      • 罗斯林和这个问题有什么关系?
      猜你喜欢
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 2012-02-07
      相关资源
      最近更新 更多