【发布时间】:2011-12-27 01:37:06
【问题描述】:
我正在尝试创建一个递归例程,它将检索指定对象下所有成员的 PropertyInfos(在 .NET 3.5 中)。直接成员的一切都在工作,但它还需要解析嵌套类(以及它们的嵌套类等)。
我不明白如何处理解析嵌套类的部分。这部分代码你会怎么写?
public class ObjectWalkerEntity
{
public object Value { get; set; }
public PropertyInfo PropertyInfo { get; set; }
}
public static class ObjectWalker
{
// This will be the returned object
static List<ObjectWalkerEntity> objectList = new List<ObjectWalkerEntity>();
public static List<ObjectWalkerEntity> Walk(object o)
{
objectList.Clear();
processObject(o);
return objectList;
}
private static void processObject(object o)
{
if (o == null)
{
return;
}
Type t = o.GetType();
foreach (PropertyInfo pi in t.GetProperties())
{
if (isGeneric(pi.PropertyType))
{
// Add generic object
ObjectWalkerEntity obj = new ObjectWalkerEntity();
obj.PropertyInfo = pi;
obj.Value = pi.GetValue(o, null);
objectList.Add(obj);
}
else
{
////// TODO: Find a way to parse the members of the subclass...
// Parse each member of the non-generic object
foreach (Object item in pi.PropertyType)
{
processObject(item);
}
}
}
return;
}
private static bool isGeneric(Type type)
{
return
Extensions.IsSubclassOfRawGeneric(type, typeof(bool)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(string)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(int)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(UInt16)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(UInt32)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(UInt64)) ||
Extensions.IsSubclassOfRawGeneric(type, typeof(DateTime));
}
编辑:我采用了 Harlam 的一些建议,并提出了一个可行的解决方案。这处理嵌套类和列表。
我已经用以下内容替换了我之前通过 propertyinfo 的循环
foreach (PropertyInfo pi in t.GetProperties())
{
if (isGeneric(pi.PropertyType))
{
// Add generic object
ObjectWalkerEntity obj = new ObjectWalkerEntity();
obj.PropertyInfo = pi;
obj.Value = pi.GetValue(o, null);
objectList.Add(obj);
}
else if (isList(pi.PropertyType))
{
// Parse the list
var list = (IList)pi.GetValue(o, null);
foreach (object item in list)
{
processObject(item);
}
}
else
{
// Parse each member of the non-generic object
object value = pi.GetValue(o, null);
processObject(value);
}
}
我还添加了一项新检查,以查看某项是否为列表。
private static bool isList(Type type)
{
return
IsSubclassOfRawGeneric(type, typeof(List<>));
}
感谢您的帮助!
【问题讨论】:
标签: c# reflection .net-3.5 recursion