Dynamically setting struct values from hash map

Andrew Chapman via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 14:30:14 PDT 2016


On Thursday, 12 May 2016 at 21:01:06 UTC, Adam D. Ruppe wrote:

> foreach(member; __traits(allMembers, YourStruct))
>   if(member in yourhash)
>    __traits(getMember, your_object, member) = 
> to!right_type(yourhash[member]);
>
>
> basically, it is a bit more complex to filter out inappropriate 
> fields and such, but that's the idea.
>
> Check out the sample chapter of my book 
> https://www.packtpub.com/application-development/d-cookbook to 
> see more, you can get the reflection chapter free on that site.

That's wonderful Adam, thank you!  I actually had your book 
previously bookmarked - I should probably buy it :-)


More information about the Digitalmars-d-learn mailing list