我正在尝试使用我的代码中的Reflection 1示例实现数据转换.
该GetSourceValue函数有一个比较各种类型的开关,但我想删除这些类型和属性,并GetSourceValue只使用一个字符串作为参数获取属性的值.我想在字符串中传递一个类和属性并解析属性的值.
这可能吗?
我试图使用反射动态地将值设置为类的嵌套属性.任何人都可以帮我这样做.
我正在上课Region.
public class Region
{
public int id;
public string name;
public Country CountryInfo;
}
public class Country
{
public int id;
public string name;
}
Run Code Online (Sandbox Code Playgroud)
我有一个Oracle数据阅读器来提供Ref游标中的值.
这会给我一个
ID,姓名,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);
Run Code Online (Sandbox Code Playgroud)
对于嵌套属性,我可以通过读取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 | …Run Code Online (Sandbox Code Playgroud)