Reflection: Get all inherited classes of a base class

Jacob Carlborg doob at me.com
Wed Dec 26 07:06:56 PST 2012


On 2012-12-25 19:26, nrgyzer wrote:

> Thanks Adam, that's exactly what I need... is it possible to call a
> static method only using TypeInfo_Class or do I need to call the
> constructor using create-method? I think using annotation would be very
> helpful for my idea:

As far as I know you cannot call any method using TypeInfo_Class.

> abstract class A {
> static abstract string myName(); // this surely doesn't work
> string[] getChildClassNames() {
> string[] retArray;
> foreach(mod; ModuleInfo) {
> foreach(cla; mod.localClasses) {
> if (cla is this.classinfo)
> retArray ~= cla.myName; // adds "Class B" and "Class C"
> }
> return retArray;
> }
> }
>
> class B : A {
> override static string myName() { return "Class B"; }
> }
>
> class C : A {
> override static string myName() { return "Class C"; }
> }
>
> But as in the most other languages it's impossible to declare abstract
> static methods and I also have no direct access from TypeInfo_Class to
> my static attributes and/or methods (or is there any chance without
> creating an instance of the class?).
>
> Hence, annotations would be very useful at this point, but is there any
> other possibility to do something like shown above...?

You can get the name of a class using "this.classinfo.name" or from any 
other TypeInfo_Class.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list