反射 - 获取嵌套对象的属性
指的是: 反射 - 设置返回 obj 的类型? 我有一个对象 Call Jobcard ,它有几个属性,其中一个是另一个名为 Customer 的对象,它有自己的属性,其中一个是另一个名为 Adress 的嵌套对象。
这两个函数也将处理其他对象类型。
private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow)
{
//Type type = dataObj.GetType();
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
//s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
private object PopulateChildObject(object dataObj, System.Data.DataRow dataRow)
{
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
问题是 PopulateChildObject 函数不起作用,因为 PropertyInfo 列表不是传递的 childObj 的列表。 如果我查看手表中传递给 PopulateChildObject 的 dataObj,它有 0 个属性。 此外,传递给 PopChildObj() 的 dataObj 具有 System.Reflection.RuntimePropertyInfo' 类型,而不是 Customer 类型。 我缺少什么?
refers to : Reflection - setting Type of returned obj?
I have a object Call Jobcard with a few properties, one of which is another object called Customer with its own properties, one of which is another nested object called Adress.
These 2 functions will be handling other object types as well.
private T PopulateObject<T>(T dataObj, System.Data.DataRow dataRow)
{
//Type type = dataObj.GetType();
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
//s += propertyitem.Name + ":" + (propertyitem.GetValue(dataObj,null)).ToString() + "\r\n";
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
private object PopulateChildObject(object dataObj, System.Data.DataRow dataRow)
{
System.Reflection.PropertyInfo[] proplist = dataObj.GetType().GetProperties();
foreach ( System.Reflection.PropertyInfo propertyitem in proplist)
{
if(propertyitem.Name != "")
try
{
propertyitem.SetValue(dataObj, dataRow[propertyitem.Name], null);
}
catch (Exception ex)
{
if (ex.Message.Contains("does not belong to table"))
{
propertyitem.SetValue(dataObj, PopulateChildObject(propertyitem, dataRow), null);
}
else
throw;
}
}
return dataObj;
}
The problem is that the PopulateChildObject function does not work because the PropertyInfo list is not that of the passed childObj.
If I look at the dataObj passed to PopulateChildObject in the watch, it has 0 Attributes. Also the dataObj passed to PopChildObj() has type of System.Reflection.RuntimePropertyInfo' instead of type Customer. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
propertyitem
是PropertyInfo
; 您需要将属性中的值传递给它 - 即如果该子对象是由父对象创建的,则不需要“设置”它; 只需更新底层对象:
您可能需要创建子对象(通常
Activator.CreateInstance
),在这种情况下您将需要调用SetValue
:不过,我想知道 -
PopulateObject
和PopulateChildObject
之间真的有什么区别吗? 感觉它们可能是同一件事?propertyitem
is thePropertyInfo
; you need to pass it the value from the property - i.e.If this child object is created by the parent, you shouldn't need to "set" it; just update the underyling object:
It may be you need to create the child object (usually
Activator.CreateInstance
), in which case you will need to callSetValue
:I wonder, though - is there really any difference between
PopulateObject
andPopulateChildObject
? It feels like they could be the same thing?获取 Nest 属性,例如 Developer.Project.Name
Get Nest properties e.g., Developer.Project.Name