Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

C# get all child 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));
            }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# how to simulate mouse click 
Csharp :: clone gameobject unity c# 
Csharp :: c# check if string is empty 
Csharp :: unity move left and right 
Csharp :: c# initialize dictionary 
Csharp :: unity distance between 2 vectors 2d 
Csharp :: c# get all bytes of a file 
Csharp :: smooth rotation unity 
Csharp :: unity get speed of object 
Csharp :: string to int c# 
Csharp :: csharp string to double 
Csharp :: xamarin button text lowercase 
Csharp :: remove repeated items in a list unity 
Csharp :: set text in center wpf 
Csharp :: c# change label value into int 
Csharp :: how to change scenes with button press in unity c# 
Csharp :: c# socket bind to localhost 
Csharp :: find unity 
Csharp :: c# json to dictionary 
Csharp :: if get key down unity 
Csharp :: message box in visual studio 
Csharp :: how to check if list index is out of range in c# 
Csharp :: c# array last element 
Csharp :: UnityEngine.Transform.get_position () (at <a0ef933b1aa54b668801ea864e4204fe:0) Gamekit3D.MeleeWeapon.BeginAttack (System.Boolean thowingAttack) 
Csharp :: restart wpf application 
Csharp :: Warum wächst Weizen besonders gut in den Steppen? 
Csharp :: C# inline question mark on object 
Csharp :: unity how to reorder a list 
Csharp :: how to get the ip asp.net mvc 
Csharp :: convert int to string in linq query c# 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =