7

Does anyone know of, or can recommend, a library that can recursively visualize an arbitrary object graph in .NET?

I need to be able to print out (to the console) a formatted representation of an object graph. For example, given a simple object graph like this:

var foo = new Foo();
foo.Bar = new Bar();
foo.Bar.Baz = 42;
foo.Bar.Qux = "quux";
foo.Corge = false;

It would be easy to produce output like this:

Foo:
    Bar:
        Baz: 42;
        Qux: "quux"
    Corge: false

I could definitely write such a library myself using Reflection, but if something like it already exists I might as well use it instead of wasting time on reinventing the wheel.

I need it to give coding demos, to easily show to an audience what a constructed object graph looks like.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736

3 Answers3

6

Well, this resembles JSON. You could use JavaScriptSerializer. You could also try the YAML format which is pretty human readable and there are some .NET libraries.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Yes, I ended up using Json.NET (http://json.codeplex.com/) which was good enough for my immediate purpose. However, I was wondering if there was something more general-purpose... – Mark Seemann Oct 03 '10 at 15:40
2

FWIW I found that Visual Studio ships with an Object Dumper sample that does something very close to this.

However, I find the formatting less desirable than JSON, which I ended up using instead.

Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
0

Have you tried linqpad? But then you would need to have your code in there, but that is also nice for demoing IMO.

strudso
  • 399
  • 4
  • 6
  • I don't need a tool, but I library whose methods I can invoke from my own code to show off object graph structure. Can LinqPad be used in this way? AFACT it can't, but I may be mistaken... – Mark Seemann Oct 03 '10 at 15:43