ThN
ThN

Reputation: 3276

Is there a way to load library in delphi prism?

I need to load Dynamically linked or Static library file during runtime. Is there a way to do it in delphi prism?

MSDN library doesn't seem to indicate that.

Any help or hints will be greatly appreciate it.

Thanks,

Upvotes: 0

Views: 144

Answers (2)

jamiei
jamiei

Reputation: 2036

Your question doesn't give more context on the nature of your request but if you're looking to load assemblies for a plugin type extensibility reason then I'd recommend using a library like MEF (the Managed Extensibility Framework). There is a brief post on using MEF with Delphi Prism here, but it allows you to define an interface and use your assemblies in a number of different ways:

First you should define your interface:

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

Then you can load up as many assemblies for extension as you wish in a number of different ways:

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

You then have a list of the loaded assemblies upon which you can perform your actions:

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

Upvotes: 0

RRUZ
RRUZ

Reputation: 136391

You can use the Assembly.LoadFrom method to load an assembly. from here you can use reflection to invoke a public method of the library.

Upvotes: 2

Related Questions