如何获取嵌套属性 [英] How to get nested properties
本文介绍了如何获取嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检索一个PropertyInfo,这里是代码:
I want to retrieve a PropertyInfo, Here the code :
string propertyName="Text";
PropertyInfo pi = control.GetType().GetProperty(propertyName);
它工作正常,但是如果我想检索嵌套的属性,它将返回null:
it works fine but if I want to retrieve nested properties, it returns null :
string propertyName="DisplayLayout.Override.RowSelectors";
PropertyInfo pi = control.GetType().GetProperty(propertyName);
有什么方法可以获取嵌套属性?
Is there any way to get nested properties ?
最好的问候,
弗洛里安
我现在有一个新问题,我想获取一个为数组的属性:
Edit : I have a new problem now, I want to get a property which is an array :
string propertyName="DisplayLayout.Bands[0].Columns";
PropertyInfo pi = control.GetType().GetProperty(propertyName)
谢谢
推荐答案
是:
public PropertyInfo GetProp(Type baseType, string propertyName)
{
string[] parts = propertyName.Split('.');
return (parts.Length > 1)
? GetProp(baseType.GetProperty(parts[0]).PropertyType, parts.Skip(1).Aggregate((a,i) => a + "." + i))
: baseType.GetProperty(propertyName);
}
致电:
PropertyInfo pi = GetProp(control.GetType(), "DisplayLayout.Override.RowSelectors");
递归取胜!
这篇关于如何获取嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文