Posts o[asp.net] ASP.NET equivalent of PHP var_dump
Post
Cancel

o[asp.net] ASP.NET equivalent of PHP var_dump

1
2
var json = new JavaScriptSerializer().Serialize(your_object);
Response.Write("Data:" + json);

PHP var_dump() method for C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//http://ruuddottech.blogspot.gr/2009/07/php-vardump-method-for-c.html
using System;
using System.Text;
using System.Reflection;
using System.Collections;

public string var_dump(object obj, int recursion)
{
    StringBuilder result = new StringBuilder();

    // Protect the method against endless recursion
    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...
            }
        }
    }

    return result.ToString();
}

origin - http://www.pipiscrew.com/?p=2436 asp-net-asp-net-equivalent-of-php-var_dump

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags