Getting and using class hierarhy information in runtime

Uranuz neuranuz at gmail.com
Fri Feb 14 23:49:05 PST 2014


I solved it myself. I forget that I can use function as predicate

import std.stdio, std.algorithm;

class A: Exception { this(){ super(null); } }

class B: A {}

class C: B {}

class D: A {}

class E: A {}

class F: E {}

size_t countDerivations(TypeInfo_Class typeinfo)
{	
	size_t result;
	while(typeinfo !is null)
	{	//writeln(typeinfo.name);
		result ++;
		typeinfo = typeinfo.base;
	}
	return result;
}

void main()
{
	
	auto typeinfo = typeid(C);
	//while(typeinfo !is typeid(Throwable))
	//{	writeln(typeinfo.name);
	//	typeinfo = typeinfo.base;
	//}
	
	auto list = [ typeid(C), typeid(B), typeid(A), typeid(F) ];
	
	
	//writeln( countDerivations( typeid(C) ) );
	
	auto sortedList = sort!( (a, b) { return countDerivations(a) > 
countDerivations(b); } )( list );
	writeln(sortedList);
}


More information about the Digitalmars-d-learn mailing list