In .NET & C#, suppose ClassB has a field that is of type ClassA.
One can easily use method GetFields to list ClassB\'
Try the following. It lets you control how deep you descend into the type hierarchy and should only descend into non-primitive types.
public static class FieldExtensions
{
public static IEnumerable GetFields( this Type type, int depth )
{
if( depth == 0 )
return Enumerable.Empty();
FieldInfo[] fields = type.GetFields();
return fields.Union(fields.Where( fi => !fi.IsPrimitive )
.SelectMany( f => f.FieldType.GetFields( depth -1 ) );
}
}