Retrieving the PropertyDescriptor of a nested object

1.5k views Asked by At

I have the following code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        object o;
        Person p = new Person { FirstName = "John", Surname = "Henry" };
        Citizen c = new Citizen { Country = "Canada", ResidentName = p };
        SportsFan sf = new SportsFan { Sport = "Hockey", Fan = c };

        Discoverer<SportsFan>.SimpleExample("Sport", "Hockey",out o);
        Discoverer<SportsFan>.NestedProperyExample("Fan.Citizen.FirstName", "John",out o);
    }

    private class Person
    {
        public string FirstName
        {
            get; set;
        }

        public string Surname
        {
            get; set;
        }
    }

    private class Citizen
    {
        public Person ResidentName
        {
            get; set;
        }

        public string Country
        {
            get; set;
        }
    }

    private class SportsFan
    {
        public string Sport
        {
            get; set;
        }

        public Citizen Fan
        {
            get; set;
        }
    }

    private class Discoverer<T>
    {
        public static void SimpleExample(string propName, string objResultToString,out Object obj)
        {
            PropertyDescriptor propDesc;
            propDesc = TypeDescriptor.GetProperties(typeof(T))[propName];               

            TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
            obj = converter.ConvertFromString(objResultToString);                   
        }

        public static void NestedProperyExample(string propName, string objResultToString, out Object obj)
        {
            PropertyDescriptor propDesc = null;
            obj = null;
            string[] nestedProperties = propName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);

            propDesc = TypeDescriptor.GetProperties("Form1." + nestedProperties[0])[nestedProperties[1]];
            for (int i = 1; i < nestedProperties.Length - 1; i++)
            {
                if (propDesc != null)
                    propDesc = TypeDescriptor.GetProperties(propDesc.GetType())[nestedProperties[i + 1]];
            }

            if (propDesc != null)
            {
                TypeConverter converter = TypeDescriptor.GetConverter(propDesc.PropertyType);
                obj = converter.ConvertFromString(objResultToString);
            }

        }
    }


}

The code works for the simpleExample. On the NestedPropertyExample, the first assignment to PropDesc returns null. When I check TypeDescriptor.GetProperties("Form1." + nestedProperties[0]), it returns a PropertyDescriptorCollection of one item and that is Length.

Why am I not returning more PropertyDesriptor items? Am I going about this the correct way?

Thanks, Bill N

1

There are 1 answers

0
rafailka On

The NestedProperyExample method is misspelled a bit, but don't mind -- it's not the problem (: Actually, the problem might be, that NestedProperyExample method calls the TypeDescriptor.GetProperties(Object) overload, passing it some string ("Form1." + nestedProperties[0]). As per docs (MSDN), it acts very much like just TypeDescriptor.GetProperties(typeof(string)). string's got only one simple property, its Length, -- and that's why TypeDescriptor.GetProperties does not return any more PropertyDescriptor items.

This answers your direct question, but your intentions are unclear to me. May be if you could rephrase your question and state clear what you're trying to accomplish with this code, you'll likely get a better answer.