How get struct value by member name string ?

drug007 drug2004 at bk.ru
Tue May 30 11:39:34 UTC 2023


30.05.2023 11:46, John Xu пишет:
> 
> 
> How to put above enum as a function parameter? Following code wouldn't 
> work:
> 
>      string getTMember(T t, enum string memberName) {
>          return __traits(getMember, t, memberName);
>      }
> 
> My database table is very wide, with many columns. Above ddbc allows a 
> struct
> to map db returned data. Then if I want a member's value to show in 
> vibe.d template,
> how do I use a function to get it?

This works for me:
```D
import std;

void main() {
     struct T {int a; string name;}
      auto t = T(12, "got by member name");
      enum s = "name";
      writeln(__traits(getMember, t, s));
}
```


More information about the Digitalmars-d-learn mailing list