I am searching a way to get the possible types for upcasting an object.
For example: I have a control of type MyControl which inherits Control. Now, when the object of type MyControl is downcasted to Control is there a way to find out, if it is the top object-type or when now to get the type(s) in which it can be upcasted (in this case MyControl)?
I want it upcast to MyControl (with Reflection) and get a Property with reflection. But I don’t know MyControl at the place where I have to do this.
MyControl is implement Visible with new. Now when I call control.Visible = true it will call the Visible of Control but I have to call the Visible of MyControl.
Thanks for your help.
There is:
This will work on any part of the type hierarchy. The following check will not work if the variable itself is of a base type:
However it sounds like you want the behaviour of overridden methods or properties. In the normal situation, you would override
Visible:However this is only if the base class is not sealed and the member you want to override is
abstractorvirtual. If this is not the case, then I’d stick with casting to the derived type… not ideal, but not many options.It also sounds like you tried to hide the base member like this:
This only works if you have a reference to the type itself. If you have a reference to the base type, the member hiding does not work, it doesn’t know the member is hidden in the derived type:
(In the above, if
Visiblewere overridden, then it would come from the derived class).Update: to do any of this at runtime, you can do the following so long as you know the names of the things you want to reflect: