www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Does D have reflection?

reply "dnspies" <dspies ualberta.ca> writes:
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
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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