How to load Assembly at runtime and create class instance?

Pankaj picture Pankaj · Nov 26, 2009 · Viewed 56.9k times · Source

I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also want to use the interface.

Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);

This is my code. How could it be improved?

Answer

Mehdi Golchin picture Mehdi Golchin · Nov 26, 2009

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