Correct Way to Load Assembly, Find Class and Call Run() Method

Correct Way to Load Assembly, Find Class and Call Run() Method


Use an AppDomain

It is safer and more flexible to load the assembly into its own AppDomain first.

So instead of the answer given previously:

var asm = Assembly.LoadFile(@"C:\myDll.dll");
var type = asm.GetType("TestRunner");
var runnable = Activator.CreateInstance(type) as IRunnable;
if (runnable == null) throw new Exception("broke");
runnable.Run();

I would suggest the following (adapted from this answer to a related question):

var domain = AppDomain.CreateDomain("NewDomainName");
var t = typeof(TypeIWantToLoad);
var runnable = domain.CreateInstanceFromAndUnwrap(@"C:\myDll.dll", t.Name) as IRunnable;
if (runnable == null) throw new Exception("broke");
runnable.Run();

Now you can unload the assembly and have different security settings.

If you want even more flexibility and power for dynamic loading and unloading of assemblies, you should look at the Managed Add-ins Framework (i.e. the System.AddIn namespace). For more information, see this article on Add-ins and Extensibility on MSDN.

How to load Assembly at runtime and create class instance?

If your assembly is in GAC or bin use the assembly name at the end of type name instead of Assembly.Load().

object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));

C# reflection - load assembly and invoke a method if it exists


use reflection to check if it has a method called "CustomType MyMethod(byte[] a, int b)" and call it or throw an exception otherwise

Your current code isn't fulfilling that requirement. But you can pretty easily with something like this:

var methodInfo = t.GetMethod("MyMethod", new Type[] { typeof(byte[]), typeof(int) });
if (methodInfo == null) // the method doesn't exist
{
// throw some exception
}

var o = Activator.CreateInstance(t);

var result = methodInfo.Invoke(o, params);

Is this good enough, or are there better/faster/shorter ways?

As far as I'm concerned this is the best way and there isn't really anything faster per say.

What about constructors, given that these methods are not static - can they simply be ignored?

You are still going to have to create an instance of t as shown in my example. This will use the default constructor with no arguments. If you need to pass arguments you can, just see the MSDN documentation and modify it as such.

Load a Type from Assembly using simple class name

If you don't want to use full qualifying name you need to search via LINQ and manually handle the case, where more than one class is found:

var types = assembly.GetTypes().Where(type => type.Name == "MyClass");
var firstType = types.FirstOrDefault();

Remember/lesson: there could be more than one class in assembly with the same name (classes that have different namespaces).

Calling a dynamically loaded DLL method through reflection, but the target references a DLL

Assembly.LoadFile does not resolve dependencies which leads to the exception you describe. Using Load or LoadFrom should resolve your issue.



Related Topics



Leave a reply



Submit