22

我正在尝试使用反射动态地为类的嵌套属性设置一个值。谁能帮我做到这一点。

我正在上课Region,如下所示。

public class Region
{
    public int id;
    public string name;
    public Country CountryInfo;
}

public class Country
{
    public int id;
    public string name;
}

我有一个 Oracle 数据阅读器来提供来自 Ref 游标的值。

这会给我

Id,name,Country_id,Country_name

我可以通过下面将值分配给 Region.Id、Region.Name。

FieldName="id"
prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null);

对于嵌套属性,我可以通过读取字段名创建一个实例来为下面的赋值。

FieldName="CountryInfo.id"

if (FieldName.Contains('.'))
{
    Object NestedObject = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | BindingFlags.Instance);

    //getting the Type of NestedObject
    Type NestedObjectType = NestedObject.GetType();

    //Creating Instance
    Object Nested = Activator.CreateInstance(typeNew);

    //Getting the nested Property
    PropertyInfo nestedpropinfo = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | BindingFlags.Instance);

    PropertyInfo[] nestedpropertyInfoArray = nestedpropinfo.PropertyType.GetProperties();
    prop = nestedpropertyInfoArray.Where(p => p.Name == Utility.Trim(FieldName.Split('.')[1])).SingleOrDefault();

    prop.SetValue(Nested, Utility.ToLong(reader_new[ResultName]), null);
    Nestedprop = objItem.GetType().GetProperty(Utility.Trim(FieldName.Split('.')[0]), BindingFlags.Public | BindingFlags.Instance);

    Nestedprop.SetValue(objItem, Nested, null);
}

上面的赋值给Country.Id.

但是由于我每次都在创建实例,因此Country.Id如果我选择 Next Country.Name,我将无法获得先前的值。

谁能告诉 can 为objItem(that is Region).Country.Idand赋值objItem.Country.Name。这意味着如何为嵌套属性分配值,而不是每次都创建实例和分配。

提前致谢。!

4

3 回答 3

60

您应该调用PropertyInfo.GetValue使用该Country属性来获取国家/地区,然后PropertyInfo.SetValue使用该Id属性设置该国家/地区的 ID。

所以是这样的:

public void SetProperty(string compoundProperty, object target, object value)
{
    string[] bits = compoundProperty.Split('.');
    for (int i = 0; i < bits.Length - 1; i++)
    {
        PropertyInfo propertyToGet = target.GetType().GetProperty(bits[i]);
        target = propertyToGet.GetValue(target, null);
    }
    PropertyInfo propertyToSet = target.GetType().GetProperty(bits.Last());
    propertyToSet.SetValue(target, value, null);
}
于 2012-09-06T06:40:30.470 回答
3

获取 Nest 属性,例如 Developer.Project.Name

private static System.Reflection.PropertyInfo GetProperty(object t, string PropertName)
            {
                if (t.GetType().GetProperties().Count(p => p.Name == PropertName.Split('.')[0]) == 0)
                    throw new ArgumentNullException(string.Format("Property {0}, is not exists in object {1}", PropertName, t.ToString()));
                if (PropertName.Split('.').Length == 1)
                    return t.GetType().GetProperty(PropertName);
                else
                    return GetProperty(t.GetType().GetProperty(PropertName.Split('.')[0]).GetValue(t, null), PropertName.Split('.')[1]);
            }
于 2013-11-18T16:27:07.187 回答
0

扩展 Jon Skeets 的答案,如果您得到的值是null并且您想为该类型创建一个对象:

public void SetProperty(string compoundProperty, object target, object value)
{
  var bits = compoundProperty.Split('.');
  for (var i = 0; i < bits.Length - 1; i++) {
    var propertyToGet = target.GetType().GetProperty(bits[i]);
    var propertyValue = propertyToGet.GetValue(target, null);
    if (propertyValue == null) {
      propertyValue = Activator.CreateInstance(propertyToGet.PropertyType);
      propertyToGet.SetValue(target, propertyValue);
    }
    target = propertyToGet.GetValue(target, null);
  }
  var propertyToSet = target.GetType().GetProperty(bits.Last());
  propertyToSet.SetValue(target, value, null);
}
于 2022-02-15T17:56:41.880 回答