通过反思组嵌套的属性值 [英] Set Nested Property Values using Reflections

查看:138
本文介绍了通过反思组嵌套的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜查,但始终无法找到我的问题的确切答案。就拿下面的code:

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

现在,我已经建立,有许多的文本字段用户可以在其中输入关于以公司客户信息的表格。其中一个领域是具有设置为EmailAddress的一个标签属性的电子邮件地址文本字段。我希望能够看文本框的标签,并遍历整个公司的对象找到具有匹配名称的属性,并将其值设置为文本框的Text属性。我能找到的财产​​,但设置它的价值已经被证明是相当困难的。这是我迄今:

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

    	}
    }
}

我的问题是,当我运行code我收到错误的一changeType和/或的SetValue。问题是,反思是停在INFO2,并试图将值设置为详细信息的类型 - 因为它是财产EmailAddress的父

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.

任何帮助确定如何点的SetValue到相应的属性将是有益的和AP preciated。正如我敢肯定,你也能猜到我的课是一个很大的比提供了接近100性能的例子较大。最重要的是这将是通过文本框的对象手动输入的字符串值。我试图创建一个可以然后通过各种文本框对象调用由此对象的Tag属性可以用于指示我的类属性我想设置一个例程。从那里,它是关闭的XML序列化的土地。

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.

推荐答案

你的内心行

info2.SetValue(m_objCompany, objValue, null);

正试图设置内部属性(INFO2)的值,外部对象。外对象不具有内对象

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

当你遇到,你要设置的属性,你需要创建一个属性(outerPropertyValue),然后设置该属性(通过innerPropertyValue)的属性,那么原来的对象(m_objCompany)上设置的外部性。

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

这篇关于通过反思组嵌套的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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