Dynamic Assemblies loading using Reflection

Posted April 24th, 2009 by Juozas

Tuesday I’ve posted post how to use .Net assemblies in PHP, however, as Teal’c correctly pointed out:

.NETMaking your library COM visible requires that you register the DLL in the GAC and registering something in the GAC should only be done when multiple applications need to access the same version of the same library. Even then, most software houses will deploy each app with it’s own copy of the DLL instead of using the GAC.

Nevertheless, C# (and most of other languages) support Reflection. Basically using reflection, programs can “observe and modify its own structure and behavior” (wiki). .NET has System.Reflection namespace with all needed methods. Purpose of this post is to find a way how to load DLL’s dynamically – without worrying about registering, re-registering, unregistering them in running system/production servers.

DLL’s are library assemblies, which “contain code in CIL, which is usually generated from .NET languages, and then compiled into machine language at runtime by the CLR just-in-time compiler.” (from). For all not familiar with assemblies, assembly “is a reusable, versionable, and self-describing building block of a common language runtime application.” (from). Consider this code:

using System;
using System.Reflection;
 
namespace Php
{
    public class Reflection
    {
        public Object get(string assembly, string className)
        {
            Assembly asm = Assembly.LoadFrom(assembly);
 
            return asm.CreateInstance(className, true);
        }
    }
}

This is very basic class, which can be used to do such things as:

Php.Reflection obj = new Php.Reflection ();
Object my = obj.get("my.dll", "namespace.class");

You may have thought that this class is absolutely useless, because you can simply use System.Reflection.Assembly without wrapping it into separate class. However, I was writing this code with an intention to use it as COM object in PHP and because PHP can’t access static COM class methods (which I find very disappointing) I needed to create this class. Now in PHP you can write:

$obj = new Com ("Php.Reflection");
$math = $obj->get("math_class.dll", "phpclass3.first");

dotnet_global_assembly_cacheThis method, in my opinion, is much much more better than registering every single class with global assembly cache. It’s more practical and maintainable to have all required libraries in the same project directory, and not somewhere in Windows core. However, benchmarks are still coming so stay tuned.

* For everyone interested in Winphp contest, and people building PHP apps in Windows, I recommend reading www.eurowinphp.com blog, which aggregates all participants blogs. For example, Timmy Kokke, another WinPHP participant, recently wrote another great tutorial how to make your .NET classes accessible in PHP. Also, you can follow my on Twitter at twitter.com/juokaz.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">