Getting member by name in runtime

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


On Wednesday, 21 August 2013 at 13:00:40 UTC, John Colvin wrote:
> 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.

struct A
{
     int a,b,c,d,e,f,g,h,i,j,k;
}

A a;
auto byString = string_access!int(a);

string name = //some RT val
int val = //some RT val

byString[name] = val;


More information about the Digitalmars-d-learn mailing list