March 30, 2009 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 |
March 30, 2009 Re: dump object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Qian Xu | 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 |
Copyright © 1999-2021 by the D Language Foundation