c# get all inherited classes of a class
//through reflection
using System.Reflection;
//as a reusable method/function
Type[] GetInheritedClasses(Type MyType)
{
//if you want the abstract classes drop the !TheType.IsAbstract but it is probably to instance so its a good idea to keep it.
return Assembly.GetAssembly(MyType).GetTypes().Where(TheType => TheType.IsClass && !TheType.IsAbstract && TheType.IsSubclassOf(MyType));
}
//or as a selection of a type in code.
Type[] ChildClasses Assembly.GetAssembly(typeof(YourType)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(YourType))));
//Example of usage
foreach (Type Type in
Assembly.GetAssembly(typeof(BaseView)).GetTypes()
.Where(TheType => TheType.IsClass && !TheType.IsAbstract && TheType.IsSubclassOf(typeof(BaseView))))
{
AddView(Type.Name.Replace("View", ""), (BaseView)Activator.CreateInstance(Type));
}
Also in C#:
- asp.net core timeout
- unity accessing 2d pointlight from c# script
- leantween id
- how to convert a number to 2 decimal places in c#
- array to list C
- hash sign c sharp
- There is already an open DataReader associated with this Command which must be closed first c#
- blazor display validation message
- unity rigidbody constraints
- C# get all child classes of a class
- convert double to currency c#
- unity instantiate
- unity particle system color
- c# string array
- c# convert to nullable datetime
- c# md5 string
- Globalization and localization in ASP.NET Core
- c# typeof
- unity left mouse button
- c# string to float
- convert number of days into months c#
- application.stop unity
- sort c#
- c# how to initialize an array