downcasting Object types
-
Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)
Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal -
Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)
Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? Bilal -
What's the definition of MyClass and MyAbstractClass? Also, have you tried this without using CreateInstance to see if that works? Cheers, Simon "VB.NET ... the STD of choice", Me, interal company memo
MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal
-
Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)
Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? BilalCan't imagine why this wouldn't work. Try this:
MyAbstractClass x = obj as MyAbstractClass;
as
is a lot more flexible than direct casting and if it can't cast then rather than throwing an exception it will set x tonull
, giving you some room to debug. Paul I think there're pieces of me you've never seen - Tori Amos, Tear in Your Hand -
MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal
You might want to look at the Activator class. Note that if you want to cast to an abstract class you own, the class will have to refer to the same abstract class (ie same assembly and same version)
-
MyClass is derived from MyAbstractClass (sorry, should have mentioned that) and implements parent's abstract functions. What alternatives are there for creating objects at runtime from definitions available only in other .NET DLLs or EXEs? I'm just a .NET newbie :-O Bilal
As Eric allueded to, you've got to watch out when using objects from other assemblies. Unless you're using an interface or class type shared from the same .dll, you'll run into problems. In other words, the assembly creating the object and the assembly trying to downcast the object must both reference a separate assembly that describes the interface or class to which you are downcasting. (Does that make sense?) :D John
-
As Eric allueded to, you've got to watch out when using objects from other assemblies. Unless you're using an interface or class type shared from the same .dll, you'll run into problems. In other words, the assembly creating the object and the assembly trying to downcast the object must both reference a separate assembly that describes the interface or class to which you are downcasting. (Does that make sense?) :D John
-
Someone please tell me if this is supposed to work: (because it compiles and all I'm getting is a runtime invalid cast exception)
Object obj = someAssembly.CreateInstance("MyClass"); MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism
More importantly, I don't want to use Invoke() calls. Isn't there a cleaner work-around? BilalThe following is more safer:
Object obj = someAssembly.CreateInstance("MyClass"); if( obj is MyAbstractClass ) { MyAbstractClass x = (MyAbstractClass) obj; //downcasting allowed?? x.doCoolStuff(); //exploit polymorphism }
and the precondition is you have added reference library, which has a definition of MyAbstractClass.:cool: I'm amumu, and you?