例子:
public class B
{
[IsSearchable]
[IsEncryptable]
public string bPropA { get; set; }
[IsSearchable]
[IsEncryptable]
public string bPropB { get; set; }
}
class A
{
[IsSearchable]
[IsEncryptable]
public string PropA { get; set; }
[IsSearchable]
[IsEncryptable]
public string PropB { get; set; }
[IsSearchable]
public int PropC { get; set; }
[IsSearchable]
public B PropD { get; set; }
}
因此,在上面的示例中,当将 A 类对象发送到函数时,我需要所有 6 个属性。到目前为止,我已经尝试过:
A a = new A();
a.GetType().GetAllProperties();
但它只返回 4 个属性而不是它的成员属性。环顾四周,但找不到任何关于此事的问题,至少不是具体的。
我想你需要做类似的事情:
(未经测试)
List<PropertyInfo> GetPropertiesRecursive(Type type)
{
var properties = new List<PropertyInfo>
foreach(var propertyInfo in type.GetProperties())
{
properties.Add(propertyInfo);
if(!propertyInfo.PropertyType.IsValueType)
{
properties.AddRange(GetPropertiesRecursive(propertyInfo.PropertyType));
}
}
return properties;
}
请注意,值类型也可以具有属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句