.NET, C#, Reflection: list the fields of a field that, itself, has fields

前端 未结 5 1788
轻奢々
轻奢々 2020-12-11 08:53

In .NET & C#, suppose ClassB has a field that is of type ClassA. One can easily use method GetFields to list ClassB\'

5条回答
  •  既然无缘
    2020-12-11 09:24

    You need to write a recursive method that takes an object, loops through its fields (obj.GetType().GetFields()), and prints the value of a field of primitive type, and calls itself for a class (other than String).

    You'll need a parameter for the indent size for use with recursion.

    EDIT: You'll also need some mechanism to prevent a stack overflow for cyclic object graphs. I recommend placing a limit on the indent parameter.

提交回复
热议问题