Serialization and printing of the entire state of the object during debugging

When debugging an ASP.NET application, I want to get a listing of the entire state of a very large object. I want all the properties and values โ€‹โ€‹in this object and the same for each property object to be recursive.

Since the external interface of the application expires after a significant delay, I cannot add a clock or use the Immediate window or move the cursor over an object, since there will not be enough time to fully examine the object.

Is there a way to get a full listing of an object in debug mode or, say, a C # utility or function that does this?

+4
source share
2 answers

You can use reflection to get a list of all the properties and fields of the class type, and then use them to get the runtime values โ€‹โ€‹of each of these properties / values โ€‹โ€‹and spit them out to the console.

The PropertyInfo type ( here ) and the FieldInfo type ( here ) are what you need to get from the Type object for your own instance of the class.

 MyObject myObject = ... //setup my object Type myType = myObject.GetType(); //or Type.GetType(myObject); //I think PropertyInfo[] properties = myType.GetProperties(); FieldInfo[] fields = myType.GetFields(); properties[0].GetValue(myObject); //returns the value as an Object, so you may need to cast it afterwards. 
+1
source

Reflection is actually your best bet here. You can start from your root object, get all its properties and their values, and, if necessary, return properties and values โ€‹โ€‹from these values โ€‹โ€‹recursively. This is a really powerful technique, and if you do not already know it, you probably should study it, and it gives a great project to learn. :)

0
source

Source: https://habr.com/ru/post/1301065/


All Articles