D
DaTurk
Hi,
This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I'm
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.
So I was hoping to have an instance of the base class, and cast it
between the children depending on what's decided during runtime. But
I'm not sure how to represent the class so it can be cast, and chaged.
I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don't know how to represent the class
so the cast will work.
So like this,
System.Type childA = typeof(childA);
System.Type childB = typeof(childB);
System.Type generalChild = "which ever child the user wantes";
Parent parent = null;
//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"
But I can't figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.
This is a question brought about by a solution I came up with to
another question I had, which was "Dynamic object creation". So, I'm
curious if you can dynamically cast an object. If you have two object
which have a common base class, they can both be cast up to the base
class, but if either of the child classes have unuque methods you will
not be able to access them. Now I know about late binding and all that
stuff, but I need a much more light weight solution.
So I was hoping to have an instance of the base class, and cast it
between the children depending on what's decided during runtime. But
I'm not sure how to represent the class so it can be cast, and chaged.
I originally thought to have System.Type variables representing the two
child classes, and then a third System.Type which I would = to which
ever of the child types was chosen, and that third one could be the
"cast to" class variable, but I don't know how to represent the class
so the cast will work.
So like this,
System.Type childA = typeof(childA);
System.Type childB = typeof(childB);
System.Type generalChild = "which ever child the user wantes";
Parent parent = null;
//later in the program
((generalChild)parent)."the correct method for the child in
generalChild"
But I can't figure out how to represent a class in a way, form a type,
that the system will let this compile. THanks in advance for the help.