In .NET & C#, suppose ClassB
has a field that is of type ClassA
.
One can easily use method GetFields
to list ClassB
\'
The class that does this already exists! Take a look at the Microsoft C# Samples for Visual Studio: http://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=csharpsamples&ReleaseId=8
Specifically, look at the ObjectDumper sample as it goes n-levels deep. For example:
ClassB myBObject = new ClassB();
...
ObjectDumper.Write(myBObject, Int32.MaxValue);
//Default 3rd argument value is Console.Out, but you can use
//any TextWriter as the optional third argument
It has already taken into account whether an object in the graph has been visited, Value types vs. object types vs. enumerable types, etc.