digitalmars.D.learn - Does D have reflection?
- dnspies (5/5) Apr 05 2014 Is there a way to obtain field and method names (as strings) or
- Adam D. Ruppe (7/9) Apr 05 2014 Yes, you can use __traits(allMembers) at compile time to build a
Is there a way to obtain field and method names (as strings) or assign to fields (of structs or classes) if the string is a name which is only known at runtime? For instance, could I write a generic function to reconstruct any object of any type from a JSON object?
Apr 05 2014
On Sunday, 6 April 2014 at 02:25:32 UTC, dnspies wrote:For instance, could I write a generic function to reconstruct any object of any type from a JSON object?Yes, you can use __traits(allMembers) at compile time to build a runtime function that does the setting. .tupleof can do it too, which is what I used in the fromUrlParam function in my web.d that populates a struct from an associative array of runtime strings: https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/web.d#L2507
Apr 05 2014