For example, ControlsCollection class returns Control type objects. I would like to access properties of TextBox and DropDownList type objects. In this case that would be Text and SelectedIndex respectively. So far I used casting at design-time, but I had to know what object types will be stored in the collection in advance.
It is not always possible to know what type of object will be stored in a collection at a run-time. What is a convinent way to downcast objects dynamically at run-time? If I use GetType() method I can get its Type, but how to downcast it from there and access a derived class object members?
The problem extends with method definitions. If a parameter is of a base class type it accepts derived class objects. But how to access derived class object members within the method that is passed as a parameter of a base class type. Of course, the assumption is that you do not know what subclass objects will be passed at the run-time.
What is the best way to deal with this type of downcasting?