Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Child classes from base class

Tags:

Is it possible in C# to get types of subclasses from base class?

like image 321
Polaris Avatar asked Apr 30 '10 07:04

Polaris


People also ask

How can we get derived class from base class?

If you're trying to get the base class name, it'd be something like: Type classType = typeof(YourClass); Type baseType = classType. BaseType; string baseClassName = baseType.Name; Note that, if you recursively search the base types, when you call BaseType on typeof(System.

Can a parent class access child class?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.

Can a parent have more than one child class class?

Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass.

Can a child class have a child class?

Hierarchies. Multiple classes can be derived from a single parent. There is no limit to the number of children a class can have (but a child can have only one parent).


1 Answers

You can do this:

var subclassTypes = Assembly    .GetAssembly(typeof(BaseClass))    .GetTypes()    .Where(t => t.IsSubclassOf(typeof(BaseClass))); 
like image 92
H77 Avatar answered Nov 03 '22 14:11

H77