Getting member by name in runtime

Jacob Carlborg doob at me.com
Wed Aug 21 05:03:37 PDT 2013


On 2013-08-21 10:10, 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 don't think so. Unless you build a set or associative array at compile 
time with all the members of a given type.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list