Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Assembly at runtime and create class instance?

Tags:

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?

like image 660
Pankaj Avatar asked Nov 26 '09 13:11

Pankaj


People also ask

Could not load a file or assembly?

Http 5.2. 0.0? In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

What is System reflection assembly?

Namespace: System.Reflection. Summary. Defines an Assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.


1 Answers

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));
like image 117
Mehdi Golchin Avatar answered Sep 19 '22 12:09

Mehdi Golchin