Getting member by name in runtime

John Colvin john.loughran.colvin at gmail.com
Wed Aug 21 06:00:27 PDT 2013


On Wednesday, 21 August 2013 at 08:08:56 UTC, Marek Janukowicz 
wrote:
> I get a string from HTTP request and I need to process a member 
> of this name
> of an object. I would like to use __traits(getMember) for that, 
> but of
> course it doesn't work with runtime parameters. So what I ended 
> up with is:
>
> foreach( s; __traits(derivedMembers, typeof(obj))) {
>   if (s == name) ...
> }
>
> Of course this looks suboptimal as I run the loop until I find 
> a member
> matching the name. Is there any better way to do this?

I wrote something for this a while back for loading config files:

https://github.com/John-Colvin/ES/blob/master/JCutils.d#L195

You'll need everything from that line downwards.


More information about the Digitalmars-d-learn mailing list