Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assembly.CreateInstance invalid cast Exception.

Status
Not open for further replies.

elguaro

Programmer
Nov 13, 2002
10
0
0
AU
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.

 
It returned true for me..but i was not able to look into the exception part..
 
Thanks. I got working by loading the assembly using it's full name instead of the path ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top