Cast derived class type dynamically to base class type C#

Mayank Pathak
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.

Patrick Hofman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cast derived class type dynamically to base class type C#

From Dev

Cast derived class to base

From Dev

Method in base class that returns derived class type?

From Dev

Identifying derived class type from a base class

From Dev

Use Base class type or Derived class type in for each loop

From Dev

Cast base object to derived class

From Dev

Cast object to base or derived class?

From Dev

How to return derived type while operating over base and derived class?

From Dev

convert/cast base type to derived generic type

From Dev

Derived typescript class type not accepted as it's own base type

From Java

Is there a way to get a derived class' type in a base class' function?

From Dev

Conflict in return type from base class with derived class using auto

From Dev

Use derived class type as parameter of generic action of base class

From Dev

error in derived class 'no type named my_data in <base class>'

From Dev

Save derived class type after casting it to base class

From Dev

Can a Base Class Method return the type of the derived class?

From Dev

"this" keyword type when called on an object of derived class from base class

From Dev

Type of error for assigning Base class objects with Derived class objects

From Dev

Cannot add a derived class object to a list of it's base class type

From Dev

Accessing derived class methods from a vector of type base class

From Dev

Call function from Derived class stored in a Map with Base class type

From Dev

Is there a way to declare members of an abstract base class that are the type of the derived class?

From Dev

C# - Type conversion issue when calling base class method from derived class

From Dev

Call a method of a derived class from an array of pointers of type base class. (C++)

From Dev

C# - Type conversion issue when calling base class method from derived class

From Dev

Creating variable of type <derived class> to store <base class> object in C#

From Dev

Can derived object cast to base reference in c++ template class?

From Dev

Type cast an instance of a class

From Dev

Cast a Type to a class

Related Related

  1. 1

    Cast derived class type dynamically to base class type C#

  2. 2

    Cast derived class to base

  3. 3

    Method in base class that returns derived class type?

  4. 4

    Identifying derived class type from a base class

  5. 5

    Use Base class type or Derived class type in for each loop

  6. 6

    Cast base object to derived class

  7. 7

    Cast object to base or derived class?

  8. 8

    How to return derived type while operating over base and derived class?

  9. 9

    convert/cast base type to derived generic type

  10. 10

    Derived typescript class type not accepted as it's own base type

  11. 11

    Is there a way to get a derived class' type in a base class' function?

  12. 12

    Conflict in return type from base class with derived class using auto

  13. 13

    Use derived class type as parameter of generic action of base class

  14. 14

    error in derived class 'no type named my_data in <base class>'

  15. 15

    Save derived class type after casting it to base class

  16. 16

    Can a Base Class Method return the type of the derived class?

  17. 17

    "this" keyword type when called on an object of derived class from base class

  18. 18

    Type of error for assigning Base class objects with Derived class objects

  19. 19

    Cannot add a derived class object to a list of it's base class type

  20. 20

    Accessing derived class methods from a vector of type base class

  21. 21

    Call function from Derived class stored in a Map with Base class type

  22. 22

    Is there a way to declare members of an abstract base class that are the type of the derived class?

  23. 23

    C# - Type conversion issue when calling base class method from derived class

  24. 24

    Call a method of a derived class from an array of pointers of type base class. (C++)

  25. 25

    C# - Type conversion issue when calling base class method from derived class

  26. 26

    Creating variable of type <derived class> to store <base class> object in C#

  27. 27

    Can derived object cast to base reference in c++ template class?

  28. 28

    Type cast an instance of a class

  29. 29

    Cast a Type to a class

HotTag

Archive