2012-09-04 49 views
0

我需要在運行時加載動態鏈接或靜態庫文件。有沒有辦法做到這一點在德爾福棱鏡?有沒有辦法在delphi prism中加載庫?

MSDN庫似乎並沒有表明這一點。

任何幫助或提示將不勝感激。

謝謝,

回答

0

你的問題沒有給出更多關於請求性質的上下文,但是如果你想爲插件類型的可擴展性原因加載程序集,那麼我建議使用像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; 
相關問題