在不知道属性名称的情况下查找Object属性中的值

时间:2012-03-11 00:30:35

标签: c# .net visual-studio-2010 object

我有一个Bank对象,它有几个嵌套的对象/属性/方法,以及环绕com对象。此lib的文档有限,我想更改对象中的银行编号,但我不知道属性的名称是什么,或者对象树中有多远。但是我知道该属性的值是1231241.如何在对象中搜索该值以查找并更改它?

我试过了:

  • 挖掘观察窗口,但对象很大
  • Bug Aid这仍然处于测试阶段且停止工作
  • Object Compare但由于com对象
  • ,我觉得这不行

我可能只需要弄脏我的手并在观察窗口挖掘,但我想我会看到是否有人有任何想法。我正在寻找一个实用程序。

1 个答案:

答案 0 :(得分:1)

以下是我使用Reflection创建的示例,可能对您有所帮助:

private static void recurseAndFindProperty(Object obj) {
   foreach (PropertyInfo pi in obj.GetType().GetProperties()) {
       if ((pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))) {
           IEnumerable collection = (IEnumerable)pi.GetValue(obj, null);

           foreach (object val in collection)
               recurseAndFindProperty(val);
       } else {
            if (pi.PropertyType != typeof(Descendant))
                if ((int)pi.GetValue(obj, null) == 1231241)
                    pi.SetValue(obj, 10, null)); // Change the value.
       }
   }
}