I have a compex object
class A
{
int Field1;
int field2;
property ClassB ClassB;
property classC classC;
etc etc....
}
I want to print the complete object graph using reflection. Any good code out there?
I have a compex object
class A
{
int Field1;
int field2;
property ClassB ClassB;
property classC classC;
etc etc....
}
I want to print the complete object graph using reflection. Any good code out there?
I kinda like mine better, since it compiles. I've implemented it as a series of extension methods and static stuff, so put this in a static class somewhere in your project, and the extension methods will be instantly available once you include the namespace that contains the static class. Here's how you use it:
It will recursively tunnel all the way down through your graph until it finds something it can Convert.ToString(), which it will then Debug.Print out to your immediate window. Here is some sample output:
It only prints public properties, though.
Here's the PrintGraph method:
And here's the infrastructure you need to get it to work:
I did something for debugging purpose some years ago. It's a recursive function that print all properties and sub object. The way you print is up to you. Just put the code you want in the print method. It's not "bullet proof" but it works pretty well :
Hope it helps
A minimalist alternative, capable of displaying complex objects in a readable format: