【发布时间】:2014-12-06 19:45:41
【问题描述】:
我正在尝试 (1) 记录对象的所有属性,以及 (2) 其中特定对象类型的所有属性。我可以做(1)但不能做(2)。
现在就是这样。
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(object1))
{
string name = descriptor.Name;
object value = descriptor.GetValue(object1);
logger.Debug(String.Format("{0} = {1}", name, value));
}
我需要的是这样的:
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(object1))
{
string name = descriptor.Name;
object value = descriptor.GetValue(object1);
logger.Debug(String.Format("{0} = {1}", name, value));
// TODO check if the current property of object1 is of type object2, how?
if (...) {
// TODO repeat the process for object2
foreach (PropertyDescriptor innerdescriptor in TypeDescriptor.GetProperties(object2))
{
string innername = innerdescriptor.Name;
object innervalue = innerdescriptor.GetValue(object2);
logger.Debug(String.Format(" {0} = {1}", innername, innervalue));
}
} // end if
}
但是,无论我尝试什么,第二件事都不起作用。所以,请帮忙。
更新 我对支票有答案(@Alex Art.)
if (descriptor.PropertyType == typeof(the type that you expecting) ) { ... }
现在唯一剩下的就是内部对象属性记录器了!
【问题讨论】:
标签: c# properties inner-classes