I am trying to load a class (say Class1:Interface ) without knowing its name from an assembly. My code looks like this:
Assembly a = Assembly.LoadFrom("MyDll.dll");
Type t = (Type)a.GetTypes()[0];
(Interface) classInstance = (Interface) (ClasActivator.CreateInstance(t);
Based on the articles I found online and MSDN, GetTypes()[0]
is suppose to return Class1 (there is only one class in MyDll.dll). However, my code returns Class1.Properties.Settings
. So line 3 creates an exception:
Unable to cast object of type 'Class1.Properties.Settings' to type Namespace.Interface'.
I really do not know why and how to fix this problem.