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

发布于 2024-11-06 04:31:59 字数 1980 浏览 0 评论 0原文

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

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

编辑我已经工作的问题:

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

![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 被单击”。我知道我可以通过名称获取它,但如果我可以实现这个调试器功能,那就太好了。看看下图中它会变得多么复杂。

在此处输入图像描述

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

Editing the question I have worked:

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

enter image description here

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.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-11-13 04:31:59

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

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

或者您可能想要递归打印属性,直到一定级别:

    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 级。

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

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

            }
        }
    }

EDIT: Calling the above method:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文