查找对象的所有属性和子属性

Ton*_*Nam 3 c# reflection

有时我想知道一个对象是否有我正在寻找的属性,但有时一个对象有很多属性,可能需要一些时间才能找到它进行调试.如果我能编写一个能够在字符串中找到所有属性及其值的函数,那么我可以将该字符串粘贴到记事本中,然后使用记事本具有的查找功能查找我要查找的值.到目前为止,我有这样的事情:

public void getAllPropertiesAndSubProperties(System.Reflection.PropertyInfo[] properties)
        {
            foreach (var a in properties)
            {
                //MessageBox.Show(a.ToString());
                // do something here to test if property a is the one 
                // I am looking for
                System.Reflection.PropertyInfo[] temp = a.GetType().GetProperties();
                // if property a has properties then call the function again
                if (temp.Length > 0) getAllPropertiesAndSubProperties(temp);
            }
        }
Run Code Online (Sandbox Code Playgroud)

编辑我工作过的问题:

到目前为止,我已添加以下代码.我可以将我想要的任何对象传递给以下方法,我可以看到所有属性.我无法查看属性的值

![public void stackOverex(dynamic obj)
{
    // this is the string where I am apending all properties
    string stringProperties = "";

    Type t = obj.GetType();
    List<PropertyInfo> l = new List<PropertyInfo>();
    while (t != typeof(object))
    {
        l.AddRange(t.GetProperties());
        t = t.BaseType;

        var properites = t.GetType().GetProperties();
        foreach (var p in properites)
        {
            string val = "";
            try
            {
                val = obj.GetType().GetProperty(p.Name).GetValue(obj, null).ToString();
            }
            catch
            {

            }
            stringProperties += p.Name + " - " + val + "\r\n";
        }

    }

    MessageBox.Show(stringProperties);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

是的Visual Studio调试器很棒,但看看对象可以拥有多少属性.我实际上正在寻找gridViewColumnHeader的indexSomething属性我不记得我记得以前使用它的确切名称.我有一个事件,当一个列被点击时触发,我想知道索引而不是名称"列号2?或3被点击".我知道我可以用它的名字来获取它,但如果我可以实现这个调试器函数会很好.看看下面的图片有多复杂.

在此输入图像描述

Mig*_*elo 6

如果您想要包括基本类型在内的所有属性,那么您可以这样做:

        Type t = typeof(AnyType);
        List<PropertyInfo> l = new List<PropertyInfo>();
        while (t != typeof(object))
        {
            l.AddRange(t.GetProperties());
            t = t.BaseType;
        }
Run Code Online (Sandbox Code Playgroud)

或者你想要一个递归的属性打印,达到一个级别:

    public static void ReadALotOfValues(StringBuilder b, object o, int lvl, int maxLvl)
    {
        Type t = o.GetType();
        List<PropertyInfo> l = new List<PropertyInfo>();
        while (t != typeof(object))
        {
            l.AddRange(t.GetProperties());
            t = t.BaseType;
        }
        foreach (var item in l)
        {
            if (item.CanRead && item.GetIndexParameters().Length == 0)
            {
                object child = item.GetValue(o, null);
                b.AppendFormat("{0}{1} = {2}\n", new string(' ', 4 * lvl), item.Name, child);
                if (lvl < maxLvl)
                    ReadALotOfValues(b, child, lvl + 1, maxLvl);

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:调用上面的方法:

object o = ...some object here...;
var b = new StringBuilder();
ReadALotOfValues(b, o, 0, 5);
Console.WriteLine(b.ToString());
Run Code Online (Sandbox Code Playgroud)

以上内容将读取objeto中最多5个深度级别的属性.

必须以某种方式限制搜索,否则它将永远循环...想到一个自引用对象.