C# 通用对象调试器

C# 通用对象调试器,c#,C#,我正在尝试编写一个通用函数,我可以将一个对象传递给它,它将打印出c#中的所有属性和值 我已经尝试了很多例子,比如,还有一些像 public void PrintProperties(object obj) { PrintProperties(obj, 0); } public void PrintProperties(object obj, int indent) { if (obj == null) return;

我正在尝试编写一个通用函数,我可以将一个对象传递给它,它将打印出c#中的所有属性和值

我已经尝试了很多例子,比如,还有一些像

    public void PrintProperties(object obj)
    {
        PrintProperties(obj, 0);
    }
    public void PrintProperties(object obj, int indent)
    {
        if (obj == null) return;
        string indentString = new string(' ', indent);
        Type objType = obj.GetType();
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propValue = property.GetValue(obj, null);
            if (property.PropertyType.Assembly == objType.Assembly)
            {
                Console.WriteLine("{0}{1}:", indentString, property.Name);
                PrintProperties(propValue, indent + 2);
            }
            else
            {
                Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
            }
        }
    }

但我希望在调试/日志文件中的某些对象包含string[]属性。所有这些示例都将这些输出为

System.String[]
如果我有一个像

class Thing
{
    public string Name { get; set; }

    public int Number { get; set; }

    public string[] Names { get; set; }
}
我希望在日志中看到设置了任何值的bellow

Name: Test
Number: 3
Names[0]: Fred
Names[1]: John
Names[2]: Jimmy
感谢您的帮助=]

这就是我最后使用的类

class Descriptor
{
    public void PrintProperties(object obj)
    {
        PrintProperties(obj, 0);
    }
    public void PrintProperties(object obj, int indent)
    {
        if (obj == null) return;
        string indentString = new string(' ', indent);
        Type objType = obj.GetType();
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propValue = property.GetValue(obj, null);

            if (propValue.GetType().IsArray)
            {
                object[] arrary = (object[]) propValue;
                foreach (string value in arrary)
                {
                    if (property.PropertyType.Assembly == objType.Assembly)
                    {
                        Console.WriteLine("{0}{1}:", indentString, property.Name);
                        PrintProperties(value, indent + 2);
                    }
                    else
                    {
                        Console.WriteLine("{0}{1}: {2}", indentString, property.Name, value);
                    }
                }
                continue;
            }

            if (property.PropertyType.Assembly == objType.Assembly)
            {
                Console.WriteLine("{0}{1}:", indentString, property.Name);
                PrintProperties(propValue, indent + 2);
            }
            else
            {
                Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
            }
        }
    }

}

现在我将在这个类中使用Log4Net,现在在我的mvc3站点中,我可以调用它,在打开时提供和发布ViewModels以获得全面的调试

如果您不介意使用Windows窗体,有一个名为
PropertyGrid
的控件,它基本上满足您的需求:


现在,对于您的特定问题,问题是您没有查看数组内部。您应该做的是查看每个属性的类型。如果是数组类型,则需要将值强制转换为
对象[]
数组,然后迭代每个元素,而不是简单地打印值的
ToString()
输出。您还需要创建一个递归算法,该算法查看每个属性的内部,并将其视为具有要迭代的属性的对象。如果您需要帮助,请告诉我。

嘿,谢谢您的快速回复。如果你能给我举个很好的例子。谢谢。将值强制转换为对象数组,然后遍历每个元素,而不是简单地打印值的ToString()输出。创建一个递归算法,该算法查看每个属性的内部,并将其视为具有要迭代的属性的对象。@Zac因此,当您迭代所有对象时,发现一个对象具有System.String[]属性,您检测到它,然后将该属性强制转换为对象[]并迭代数组中的所有项。
propValue.ToString().EndsWith(“[]”
不是检查结果是否为数组的好方法!我已将该行更新为
propValue.GetType()。IsArray
这样更好吗?
class Descriptor
{
    public void PrintProperties(object obj)
    {
        PrintProperties(obj, 0);
    }
    public void PrintProperties(object obj, int indent)
    {
        if (obj == null) return;
        string indentString = new string(' ', indent);
        Type objType = obj.GetType();
        PropertyInfo[] properties = objType.GetProperties();
        foreach (PropertyInfo property in properties)
        {
            object propValue = property.GetValue(obj, null);

            if (propValue.GetType().IsArray)
            {
                object[] arrary = (object[]) propValue;
                foreach (string value in arrary)
                {
                    if (property.PropertyType.Assembly == objType.Assembly)
                    {
                        Console.WriteLine("{0}{1}:", indentString, property.Name);
                        PrintProperties(value, indent + 2);
                    }
                    else
                    {
                        Console.WriteLine("{0}{1}: {2}", indentString, property.Name, value);
                    }
                }
                continue;
            }

            if (property.PropertyType.Assembly == objType.Assembly)
            {
                Console.WriteLine("{0}{1}:", indentString, property.Name);
                PrintProperties(propValue, indent + 2);
            }
            else
            {
                Console.WriteLine("{0}{1}: {2}", indentString, property.Name, propValue);
            }
        }
    }

}