I need to write a function which accepts a base class name and returns name of all its children in C#. as you all know in a namespace we have several base classes which each have their own children. Purpose of the above function (which will be located in Tools) is to get a base class name from user and then return name of all its children. (we can use the result in creating batch file) I am assuming I need to use reflection and generics but not sure how. Could you please help me about it.
How the function will look like if I pass name of base class as string? and How it should be if I pass base class name as generic type?
Edition to question after comments:
based on the page suggested in answer I tried following function but it returned null for both kids and blahKids
private object GetKidsOfBaseClass(string baseClass)
{
Type baseType = Type.GetType($"{baseClass}");
var blahKids = AppDomain.CurrentDomain.GetAssemblies().SelectMany(ass => ass.GetTypes())
.Where(p => p.IsSubclassOf(baseType));
var kids = Assembly.GetAssembly(baseType).GetTypes().Where(t => t.IsSubclassOf(baseType));
return kids;
}
Aucun commentaire:
Enregistrer un commentaire