.Net Runtime Library for Delphi
Close
How to: Load Assemblies into an Application Domain
There are several ways to load an assembly into an application domain. These are:
  • The LoadFrom method of the TClrAssembly class loads an assembly given its file location. Loading assemblies with this method uses a different load context.

  • The ReflectionOnlyLoad and ReflectionOnlyLoadFrom methods load an assembly into the reflection-only context. Assemblies loaded into this context can be examined but not executed, allowing the examination of assemblies that target other platforms. See How to: Load Assemblies into the Reflection-Only Context.

  • Methods such as CreateInstance of the TClrAppDomain class can load assemblies into an application domain.

  • The GetType method of the Type interface can load assemblies.

  • The Load method of the TClrAppDomain class can load assemblies, but is primarily used for COM interoperability. It should not be used to load assemblies into an application domain other than the application domain from which it is called.

Example

The following code loads an assembly named "example.exe" or "example.dll" into the current application domain, gets a type named Example from the assembly, gets a parameterless method named MethodA for that type, and executes the method. For a complete discussion on obtaining information from a loaded assembly, see Dynamically Loading and Using Types.

Delphi
program Asmload0; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, CNClrLib.Host, CNClrLib.Core; var AAsm: TClrAssembly; myType: _Type; myMethod: _MethodInfo; obj: OleVariant; Clrobj: _ClrObject; begin // Use the file name to load the assembly into the current // application domain. AAsm := TClrAssembly.Load('example'); // Get the type to use. myType := TClrAssembly.GetType('Example'); // Get the method to call. myMethod := myType.GetMethod_5('MethodA'); // Create an instance. obj := TClrActivator.CreateInstance(myType); // Execute the method. myMethod.Invoke_2(obj, nil); //OR //You can use the ClrObject to invoke the method // Create an instance of ClrObject. Clrobj := TClrActivator.ClrCreateInstance(myType); // Execute the method. Clrobj.InvokeMethod('MethodA'); end.