public DerivedClass : BaseClass
{
// code
}
DerivedClass B = (DerivedClass)MethodReturnsBaseClassObject();
B.DoWork();
There are many Derived classes being inherited from BaseClass
. So everytime i need to write a new method to use some of the properties.
Is this possible that using reflection we make DerivedClass
type dynamic and do something like this and make it generic.
System.Type type = GetRequestFromSession().GetType();
type B = (type )MethodReturnsBaseClassObject();
Console.WriteLine( B.Name);
Console.WriteLine( B.Age);
any help is appreciated.
This feels like bad design to me: you make something generic that you use as something that isn't. Maybe you should rethink your design.
For what your question is concerned, maybe you can create a proxy method or class that returns the type you expect. This is only useful when there is a low number of methods you call.
Another option is to use the dynamic
keyword, but I would advice only to use that if nothing else works.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments