如何使用C#反射Vaues设置为嵌套属性? [英] How to set Vaues to the Nested Property using C# Reflection.?
问题描述
我想一个值设置为类的嵌套属性动态地使用反射。谁能帮我做到这一点。
I am trying to set a value to a Nested Property of Class dynamically using reflection. Could anyone help me to do this.
我有一个类地区
象下面这样。
I am having a class Region
like below.
public class Region
{
public int id;
public string name;
public Country CountryInfo;
}
public class Country
{
public int id;
public string name;
}
我有一个Oracle数据的读者从参考光标提供的价值。
I have a Oracle Data reader to provide the Values from the Ref cursor.
这将给我作为
ID,名称,COUNTRY_ID,COUNTRY_NAME
Id,name,Country_id,Country_name
我可以能够通过以下分配值的Region.Id,Region.Name。
I could able to assign the values to the Region.Id, Region.Name by below.
FieldName="id"
prop = objItem.GetType().GetProperty(FieldName, BindingFlags.Public | BindingFlags.Instance);
prop.SetValue(objItem, Utility.ToLong(reader_new[ResultName]), null);
和为嵌套属性我可以能够通过创建一个实例做分配值到如下通过阅读字段名。
And for the Nested Property I could able to do the assign values to the as below by creating a Instance by reading the Fieldname.
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
值,如果我走了,下一步Country.Name。
But Since I am creating instance each and every time I could not able to get the previous Country.Id
value if I go for the Next Country.Name.
有谁知道能赋值给 objItem(即地区).Country.Id
和 objItem.Country.Name
。这意味着如何分配,而不是创建实例并分配每次值的嵌套属性。
Could anybody tell could to assign values to the objItem(that is Region).Country.Id
and objItem.Country.Name
. Which means how to assign values to the Nested Properties instead of creating instance and assigning everytime.
在此先感谢!
推荐答案
您应该调用 PropertyInfo.GetValue
国家属性>获得国家,那么 PropertyInfo.SetValue
使用编号
属性的设置的标识。在国家
You should be calling PropertyInfo.GetValue
using the Country
property to get the country, then PropertyInfo.SetValue
using the Id
property to set the ID on the country.
因此,像这样:
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);
}
这篇关于如何使用C#反射Vaues设置为嵌套属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!