Hi All
I'm trying to dynamically create an instance of a class. I managed to do it when that class is in the same assembly of the project I'm running, but can't get it to work when the class I want to dynamically create is in a different assembly. I've tried in many different ways, always getting the same result: invalid cast exception. This is one of the ways I'm trying:
Assembly myAssembly = Assembly.LoadFrom(somepath);
Object obj = MyAssembly.CreateInstance(
"MyNamespace.MyClass"
MyNamespace.MyClass myClass = (MyNamespace.MyClass)obj;
In the third line I get the invalid cast exception.
Just for debugging purposes I have another variable:
MyNamespace.MyClass testClass = new MyNamespace.MyClass();
If I see the type of obj it is MyNamespace.MyClass, but if I do testClass.GetType().Equals(obj.GetType()) in the command window it returns false!
Any sort of help will be highly appreciated.
I'm trying to dynamically create an instance of a class. I managed to do it when that class is in the same assembly of the project I'm running, but can't get it to work when the class I want to dynamically create is in a different assembly. I've tried in many different ways, always getting the same result: invalid cast exception. This is one of the ways I'm trying:
Assembly myAssembly = Assembly.LoadFrom(somepath);
Object obj = MyAssembly.CreateInstance(
"MyNamespace.MyClass"
MyNamespace.MyClass myClass = (MyNamespace.MyClass)obj;
In the third line I get the invalid cast exception.
Just for debugging purposes I have another variable:
MyNamespace.MyClass testClass = new MyNamespace.MyClass();
If I see the type of obj it is MyNamespace.MyClass, but if I do testClass.GetType().Equals(obj.GetType()) in the command window it returns false!
Any sort of help will be highly appreciated.