[英]How to get nested properties
我想检索一个PropertyInfo,这里代码:
string propertyName="Text";
PropertyInfo pi = control.GetType().GetProperty(propertyName);
它工作正常,但如果我想检索嵌套属性,它返回null:
string propertyName="DisplayLayout.Override.RowSelectors";
PropertyInfo pi = control.GetType().GetProperty(propertyName);
有没有办法获得嵌套属性?
最好的祝福,
弗洛里安
编辑:我现在有一个新问题,我想得到一个属性是一个数组:
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");
胜利的递归!
刚刚做同样的PropertyType
,你刚刚得到的财产(和重复往往你需要的):
PropertyInfo property = GetType().GetProperty(propertyName);
PropertyInfo nestedProperty = property.PropertyType.GetProperty(nestedPropertyName)
你可以做到,但你必须为每个级别做“整件事”,这意味着:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.