查找对象的所有属性和子属性 [英] Finding all properties and subproperties of an object

查看:61
本文介绍了查找对象的所有属性和子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sometimes I want to know if an object has a property that I am looking for but sometimes an object has a lot of properties and it may take some time to find it debugging it. It will be nice if I could write a function that will find all the properties and its values in a a string then I can paste that string in notepad for instance and look for the value that I am looking for with the find feature that notepad has. So far I have something like this:

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);
            }
        }

编辑我处理过的问题:

到目前为止,我已经添加了以下代码.我可以将想要的任何对象传递给以下方法,并且可以看到所有属性.我在查看属性值时遇到问题

so far I have added the following code. I can pass whatever object I want to the following method and I can see all the properties. I am having trouble viewing the values of the properties

![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);
}

是的,Visual Studio调试器很棒,但是可以查看一个对象可以具有多少个属性.我实际上是在寻找gridViewColumnHeader的indexSomething属性,我不记得确切的名字了,我只是记得我以前使用过它.我有一个事件在单击列时触发,我想知道索引不是名称第2列或第3列被单击".我知道我可以用名称来获得它,但是如果我可以实现此调试器功能,那将是很好的.在下面的图片中查看它的复杂程度.

Yeah visual studio debugger is great but look how many properties an object can have. I am actually looking for the indexSomething property of a gridViewColumnHeader I don't remember the exact name I just remember I have used it before. I have an event that fires when a column gets clicked and I would like to know the index not the name "column number 2? or 3 was clicked". I know I can get it with the name but it will be nice if I can implement this debugger function. See how complex it can get in the picture below.

推荐答案

如果您想要包括基本类型在内的所有属性,则可以执行以下操作:

If you want all properties including of base type then you could do this:

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

或者您可能希望递归打印属性,直到一个级别:

or maybe you want a recursive print of properties, up to a level:

    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);

            }
        }
    }

编辑:调用上述方法:

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

以上内容将读取对象中最多5个深度的属性.

The above will read properties of up to 5 levels of depth into the objeto.

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

The search must be limited somehow, otherwise it would loop forever... think of a self-referencing object.

这篇关于查找对象的所有属性和子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆