Created
October 11, 2019 14:23
-
-
Save PotterSys/0bb53bdb2aef90c4b2ade87b18ca4a3b to your computer and use it in GitHub Desktop.
var_dump adaption for C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://ruuddottech.blogspot.com/2009/07/php-vardump-method-for-c.html | |
public string var_dump(object obj, int recursion = 0) | |
{ | |
StringBuilder result = new StringBuilder(); | |
// Protect the method against endless recursion | |
if (null == obj) | |
{ | |
result.Append("<i>NULL</i>"); | |
} | |
else if (recursion < 5) | |
{ | |
// Determine object type | |
Type t = obj.GetType(); | |
// Get array with properties for this object | |
PropertyInfo[] properties = t.GetProperties(); | |
foreach (PropertyInfo property in properties) | |
{ | |
try | |
{ | |
// Get the property value | |
object value = property.GetValue(obj, null); | |
// Create indenting string to put in front of properties of a deeper level | |
// We'll need this when we display the property name and value | |
string indent = String.Empty; | |
string spaces = "| "; | |
string trail = "|..."; | |
if (recursion > 0) | |
{ | |
indent = new StringBuilder(trail).Insert(0, spaces, recursion - 1).ToString(); | |
} | |
if (value != null) | |
{ | |
// If the value is a string, add quotation marks | |
string displayValue = value.ToString(); | |
if (value is string) displayValue = String.Concat('"', displayValue, '"'); | |
// Add property name and value to return string | |
result.AppendFormat("{0}{1} = {2}\n", indent, property.Name, displayValue); | |
try | |
{ | |
if (!(value is ICollection)) | |
{ | |
// Call var_dump() again to list child properties | |
// This throws an exception if the current property value | |
// is of an unsupported type (eg. it has not properties) | |
result.Append(var_dump(value, recursion + 1)); | |
} | |
else | |
{ | |
// 2009-07-29: added support for collections | |
// The value is a collection (eg. it's an arraylist or generic list) | |
// so loop through its elements and dump their properties | |
int elementCount = 0; | |
foreach (object element in ((ICollection)value)) | |
{ | |
string elementName = String.Format("{0}[{1}]", property.Name, elementCount); | |
indent = new StringBuilder(trail).Insert(0, spaces, recursion).ToString(); | |
// Display the collection element name and type | |
result.AppendFormat("{0}{1} = {2}\n", indent, elementName, element.ToString()); | |
// Display the child properties | |
result.Append(var_dump(element, recursion + 2)); | |
elementCount++; | |
} | |
result.Append(var_dump(value, recursion + 1)); | |
} | |
} | |
catch { } | |
} | |
else | |
{ | |
// Add empty (null) property to return string | |
result.AppendFormat("{0}{1} = {2}\n", indent, property.Name, "null"); | |
} | |
} | |
catch | |
{ | |
// Some properties will throw an exception on property.GetValue() | |
// I don't know exactly why this happens, so for now i will ignore them... | |
} | |
} | |
} | |
result.Insert(0, "<pre>"); | |
result.Append("</pre>"); | |
return result.ToString(); | |
} // var_dump() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment