digitalmars.D.learn - dump object
Hi All, previously I saw an article about how to dump an object with all properties. -----------------code----------------------- void log(T)(T obj) { static if (is(T == struct) || is(T == class)){ writef("{"); foreach(i,_;obj.tupleof) { writefln("%s : %s,", obj.tupleof[i].stringof[4..$], obj.tupleof[i]); } writefln("}"); } else { writefln(obj); } } -----------------code----------------------- But this function does not work with private attributes of an object. How to print all public properties of an object? It would be very useful for debug propose. Best regards --Qian
Mar 30 2009
Qian Xu wrote:Hi All, previously I saw an article about how to dump an object with all properties. -----------------code----------------------- void log(T)(T obj) { static if (is(T == struct) || is(T == class)){ writef("{"); foreach(i,_;obj.tupleof) { writefln("%s : %s,", obj.tupleof[i].stringof[4..$], obj.tupleof[i]); } writefln("}"); } else { writefln(obj); } } -----------------code----------------------- But this function does not work with private attributes of an object. How to print all public properties of an object? It would be very useful for debug propose.Use a newer compiler version. dmd 1.041 should work. I'm successfully using this feature for serialization.Best regards --Qian
Mar 30 2009