Search code examples
c#.netinheritancereflection.net-assembly

How to find a Type that inherits from some BaseClass


I have a class

public class BaseClass
{

}

I have a child class

public class ChildClass : BaseClass
{

}

I want to know what classes/types inherit from BaseClass (find ChildClass for example). I know that i have to use assemble and reflection but don't know how/which function to use.

Edit: ok maybe now you'll get it


Solution

  • typeof(BaseClass).Assembly.GetTypes().Where(type => type.IsSubclassOf(typeof(A)));
    

    You can do this by two step:

    1. Get all types from Assembly
    2. Find only classes which is subclasses of BaseClass