Reflection: Get all inherited classes of a base class

Adam D. Ruppe destructionator at gmail.com
Sat Dec 22 14:28:56 PST 2012


On Saturday, 22 December 2012 at 22:14:28 UTC, nrgyzer wrote:
> is it possible to get a list of all inherited classes from a 
> base class like this:

Yes, though it isn't compile time - gotta be runtime.

ClassInfo[] getChildClasses(ClassInfo c) {
	ClassInfo[] info;

         // MoudleInfo is a magical thing in object.d,
         // implicitly imported, that can loop over all
         // modules in the program: user and library
	foreach(mod; ModuleInfo) {
                 // the localClasses member gives back
                 // ClassInfo things that we can compare
		foreach(cla; mod.localClasses) {
                         // note: we could also check this
                         // recursively and check
                         // cla.interfaces as well as base
			if(cla.base is c)
				info ~= cla;
		}
	}

	return info;
}


So this should be enough for your example assert, but remember 
you can't do things like templates on this, since it is all 
runtime.


More information about the Digitalmars-d-learn mailing list