Reflection in D

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 28 11:19:04 PST 2017


On Saturday, 28 January 2017 at 07:10:27 UTC, medhi558 wrote:
> I have a last question, currently i use :
> if(lc.name.indexOf("protocol.messages") != -1)

If they're all in the same module, you can also use the compile 
time reflection to scan the module for classes. That's

foreach(memberName; __traits(allMembers, mixin(__MODULE__)))
   static if(is(__traits(getMember, mixin(__MODULE__), memberName) 
: NetworkMessage) {
        if(memberName == runtime_string_of_class_name) {
           auto msg = new __traits(getMember, mixin(__MODULE__), 
memberName);
            // populate msg
        }
    }


Or something like that. The compile time stuff works well when 
everything is in a shared module.

If not, the runtime stuff has methods `base` and `interfaces` you 
can scan for your class. So like

foreach(c; mod.localClasses
   if(c.base is typeid(NetworkMessage) {
       // work with it
   }


Notice it is typeid for runtime, typeof at compile time, and this 
only gets direct children of NetworkMessage (though you could 
walk up the parent list to check more).



More information about the Digitalmars-d-learn mailing list