ilansch
ilansch

Reputation: 4878

loading assembly during run time concept

After loading an assembly, when instantiating it:

Assembly asm = Assembly.LoadFile(@"c:\file.dll");
Type type = asm.GetType("DLLTYPE");
object instance = Activator.CreateInstance(type);

How C# know the type?
From my logic, the dll should have header which define the object type.
so why is the DLLTYPE string for ?

Upvotes: 0

Views: 101

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039328

How C# know the type?

You've passed it as parameter:

Type type = asm.GetType("DLLTYPE");

so why is the "DLLTYPE" string for ?

It's the namespace and the class name that you want to instantiate:

Namespace.ClassName

Be careful because this method will return null if you make a mistake in the typename. If you want to ensure that the type exists you could use the following overload:

Type type = asm.GetType("Namespace.ClassName", true);

This will throw an exception instead of returning null which will be easier to debug instead of the NRE you would otherwise get on the Activator.CreateInstance method.

Upvotes: 3

Related Questions