【问题标题】:Is there a way to load library in delphi prism?有没有办法在delphi prism中加载库?
【发布时间】:2012-09-04 13:38:54
【问题描述】:

我需要在运行时加载动态链接或静态库文件。有没有办法在delphi prism中做到这一点?

MSDN 库似乎没有表明这一点。

任何帮助或提示将不胜感激。

谢谢,

【问题讨论】:

    标签: .net dll delphi-prism


    【解决方案1】:

    您可以使用Assembly.LoadFrom 方法来加载程序集。从这里您可以使用reflection 调用库的公共方法。

    【讨论】:

      【解决方案2】:

      您的问题没有提供更多关于您的请求性质的上下文,但是如果您出于插件类型可扩展性的原因而希望加载程序集,那么我建议您使用像 MEF 这样的库(托管可扩展性框架) .有一篇关于使用MEF with Delphi Prism here 的简短帖子,但它允许您定义接口并以多种不同方式使用您的程序集:

      首先你应该定义你的界面:

      PluginList = class
      public
        [ImportMany(typeof(IFileAction))]
        property FileActions: IList<IFileAction> read write;
        constructor;
      end;
      

      然后您可以通过多种不同方式加载任意数量的程序集以进行扩展:

      var aggregateCat := new AggregateCatalog();
      var catalogThisAssembly := new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
      var catalogIndividual := new AssemblyCatalog('plugins\MyHelloPlugin.dll');
      var dirCatalog := new DirectoryCatalog('plugins');
      
      // Add our Catalogs here,
      aggregateCat.Catalogs.Add(dirCatalog);
      
      var container := new CompositionContainer(aggregateCat);
      // Create our plugin hosting object
      var pluginList := new PluginList();
      // Compose the parts.
      container.ComposeParts(pluginList);
      

      然后您将获得一个已加载程序集的列表,您可以在这些程序集上执行您的操作:

      for each plugin: IFileAction in pluginList.FileActions do
      begin
        Console.WriteLine('Loaded ' + plugin.Name + ', version ' + plugin.Version);
      end;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多