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 ?
You’ve passed it as parameter:
It’s the namespace and the class name that you want to instantiate:
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:
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.CreateInstancemethod.