Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Nested Property Values using Reflections

I've searched throughout but can't find the exact answer to my question. Take for instance the following code:

public class Company
{
    private string m_strName;
    private Customer m_objCustomer;

    public Company()
    {
        m_strName = "";
        m_objCustomer = new Customer();
    }

    public string Name
    {
        get { return m_strName; }
        set { m_strName = value; }
    }

    public Customer CustomerInformaion
    {
        get { return m_objCustomer; }
        set { m_objCustomer = value; }
    }
}

public class Customer
{
    private string m_strName;
    private Details m_objDetails;


    public Customer()
    {
        m_strName = "";
        m_objDetails = new Details();
    }

    public string Name
    {
        get { return m_strName; }
        set { m_strName = value; }
    }

    public Details CustomerDetails
    {
        get { return m_objDetails; }
        set { m_objDetails = value; }
    }
}

public class Details
{
    private string m_strPhoneNumber;
    private string m_strEmailAddress;

    public Details()
    {
        m_strPhoneNumber = "";
        m_strEmailAddress = "";
    }

    public string PhoneNumber
    {
        get { return m_strPhoneNumber; }
        set { m_strPhoneNumber = value; }
    }

    public string EmailAddress
    {
        get { return m_strEmailAddress; }
        set { m_strEmailAddress = value; }
    }
}

Now, I have setup a Form that has many text fields where a user can enter information about a customer at a company. One of those fields is the Email Address text field that has a Tag property set to EmailAddress. I want to be able to look at the Tag of the TextBox and iterate through the entire Company object to find a property with a matching Name and Set its value to the Text property of the TextBox. I can locate the property but setting its value has turned out to be quite difficult. Here's what I have thus far:

foreach (PropertyInfo info in m_objCompany.GetType().GetProperties())
{
    if (info.PropertyType != typeof(System.String))
    {
        foreach (PropertyInfo info2 in info.PropertyType.GetProperties())
        {
            if (objTextBox.Tag.Equals(info2.Name))
            {
                if (info2.CanWrite)
                {
                    Object objValue = Convert.ChangeType(objTextBox.Text, info.PropertyType);
                    info2.SetValue(m_objCompany, objValue, null);
                }
            }

        }
    }
}

My issue is that when I run the code I receive an error at ChangeType and/or SetValue. The issue is that the Reflection is stopping at info2 and attempting to set the value to the Type of Details - since it is the parent of the Property EmailAddress.

Any help in determining how to point the SetValue to appropriate property would be helpful and appreciated. As I'm sure you well can guess my class is a GREAT deal larger than the example provided with near 100 properties. Most all are string values which will be entered in manually through TextBox objects. I'm attempting to create one routine that can then be called by all TextBox objects whereby the Tag property of the object could be used to indicate which Property of my class I'm trying to set. From there it is off to XML serialization land.

like image 276
Jeff Avatar asked Dec 22 '22 07:12

Jeff


1 Answers

your innermost line

info2.SetValue(m_objCompany, objValue, null);

is trying to set value of the inner property (info2), on the outer object. The outer object doesn't have an inner object.

What you probably want, is something like this:

    public void Bar(object m_objCompany)
    {
        foreach (PropertyInfo info in m_objCompany.GetType().GetProperties())
        {
            if (info.PropertyType != typeof(System.String))
            {
                // Somehow create the outer property
                object outerPropertyValue = info.PropertyType.GetConstructor(new Type[] { }).Invoke(new object[] { });

                foreach (PropertyInfo info2 in info.PropertyType.GetProperties())
                {
                    if ("blah" == "blah")
                    {
                        if (info2.CanWrite)
                        {
                            Object innerPropertyValue = Convert.ChangeType("blah", info2.PropertyType);
                            info2.SetValue(outerPropertyValue, innerPropertyValue, null);
                        }
                    }

                }

                info.SetValue(m_objCompany, outerPropertyValue, null);
            }
        }
    }

When you come across a property that you want to set, you need to create that property (outerPropertyValue), then setup the properties of that property (via innerPropertyValue), then set the outer property on the original object (m_objCompany).

like image 156
McKay Avatar answered Jan 04 '23 14:01

McKay