1

I want want to print all the fields in object and if one of the field is object i want to print it fields and on and on (with recursion).

I wrote function but i get this error once i've entering to recursion.

Field '_str1' defined on type 'ConsoleApplication1.StringTest' is not a field on the target object which is of type 'System.Reflection.RtFieldInfo'.

This is the Code:

static void OutPutObject(dynamic dyn,Type pType)
        {
            List<string> lFieldsList = new List<string>();

            // Get the type of MyClass.
            Type lType = pType;

            // Get the FieldInfo of MyClass.
            FieldInfo[] myFields = lType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            // Display the values of the fields.
            for (int i = 0; i < myFields.Length; i++)
            {
                if (myFields[i].FieldType.IsClass && myFields[i].FieldType.Name != "String")
                {
                    Type tType = myFields[i].FieldType.GetType();
                    OutPutObject(myFields[i], pType);
                }
                else
                {
                    Console.WriteLine(string.Format("{0}: {1}", myFields[i].Name, myFields[i].GetValue(dyn)));
                }

            }
        }




public class IntTest
{
      private int a = 1;
      private int b = 2;
}

public class StringTest
{
      private string _str1;
      private string _str2;
      private IntTest intl;

      public StringTest()
      {
          _str1 = "str1";
          _str2 = "str2";
      }
}
  • consider this: http://stackoverflow.com/questions/6309254/a-way-to-pretty-print-a-c-sharp-object – Malcolm Dec 10 '12 at 17:00

2 Answers2

1

I've change your code and now it works! Please notice that the inner classes must have default constructor.

static void OutPutObject(object obj, Type pType, bool isFirst)
        {

            // Get the FieldInfo of MyClass.
            FieldInfo[] myFields = pType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            // Display the values of the fields.
            for (int i = 0; i < myFields.Length; i++)
            {
                if (myFields[i].FieldType.IsPrimitive || myFields[i].FieldType == typeof(Decimal) || myFields[i].FieldType == typeof(String))
                {

                    if (!isFirst)
                    {
                        object temp = Activator.CreateInstance(pType);
                        obj = temp;
                    }
                    Console.WriteLine(string.Format("{0}: {1}", myFields[i].Name, myFields[i].GetValue(obj)));
                }
                else if (myFields[i].FieldType.IsClass)
                {
                    Type tType = myFields[i].FieldType;
                    OutPutObject(myFields[i], tType, false);
                }

            }
        }
Ori Erez
  • 26
  • 2
0

You can use following lines to write object public fields. I tested on .NET 4.0

   Type type = obj.GetType();

   foreach (var f in type.GetFields().Where(f => f.IsPublic)) {
       Console.WriteLine("Name: "+f.Name+" Value: "+ f.GetValue(obj) );
   }                        
hkulekci
  • 1,894
  • 15
  • 27