Dynamically Loading Assemblies

One of the more powerful features of assemblies is that they can be loaded not just statically as a reference from another assembly but also dynamically at runtime. This is accomplished by using .NET reflection to load the file from disk, find and create objects, and invoke methods. Listing 6.8 shows how this is done.

Listing 6.8. Dynamically Loading a Package
1: program DynAsm; 2: 3: {$APPTYPE CONSOLE} 4: 5: {%DelphiDotNetAssemblyCompiler '$(SystemRoot)\microsoft.net\framework\v1.1.4322\System.dll'} 6: 7: uses 8: System.Reflection; 9: 10: var 11: a: Assembly; 12: typ: System.Type; 13: meth: MethodInfo; 14: o: System.Object; 15: begin 16: // load the assembly based on the file name 17: a := Assembly.LoadFrom('D8DG.TestPkg.dll'); ...

Get Delphi for .NET Developer’s Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.