Tono Nam
Tono Nam

Reputation: 36070

Finding all properties and subproperties of an object

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

Upvotes: 4

Views: 6430

Answers (1)

Miguel Angelo
Miguel Angelo

Reputation: 24212

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.

Upvotes: 7

Related Questions