我有一个Bank对象,它有几个嵌套的对象/属性/方法,以及环绕com对象。此lib的文档有限,我想更改对象中的银行编号,但我不知道属性的名称是什么,或者对象树中有多远。但是我知道该属性的值是1231241.如何在对象中搜索该值以查找并更改它?
我试过了:
我可能只需要弄脏我的手并在观察窗口挖掘,但我想我会看到是否有人有任何想法。我正在寻找一个实用程序。
答案 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.
}
}
}