使用反射设置嵌套属性值

2024-06-22

我已经进行了彻底搜索,但找不到我的问题的确切答案。以下面的代码为例:

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

现在,我设置了一个具有许多文本字段的表单,用户可以在其中输入有关公司客户的信息。这些字段之一是“电子邮件地址”文本字段,其“标签”属性设置为“电子邮件地址”。我希望能够查看 TextBox 的 Tag 并迭代整个 Company 对象以查找具有匹配名称的属性,并将其值设置为 TextBox 的 Text 属性。我可以找到该属性,但设置其值却相当困难。到目前为止,这是我所拥有的:

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

        }
    }
}

我的问题是,当我运行代码时,我在 ChangeType 和/或 SetValue 处收到错误。问题在于反射在 info2 处停止并尝试将值设置为详细信息类型 -​​ 因为它是属性 EmailAddress 的父级。

任何帮助确定如何将 SetValue 指向适当的属性的帮助都会有所帮助并且值得赞赏。我确信您可以猜到我的类比提供的包含近 100 个属性的示例要大得多。大多数都是字符串值,将通过 TextBox 对象手动输入。我正在尝试创建一个例程,然后可以由所有 TextBox 对象调用该例程,从而可以使用该对象的 Tag 属性来指示我要设置的类的哪个属性。从这里开始,就进入了 XML 序列化领域。


你最内心的线

info2.SetValue(m_objCompany, objValue, null);

正在尝试在外部对象上设置内部属性(info2)的值。外部对象没有内部对象。

您可能想要的是这样的:

    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)上设置外部属性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用反射设置嵌套属性值 的相关文章

随机推荐