After doing many tries and research, wanted to ask away.
class Customer
{
[Foo]
public string Name {get;set;}
public Account Account {get;set;}
}
class Account
{
[Foo]
public string Info {get;set;}
}
I am trying to get all the properties marked with [Foo]
attribute.
I have done some recursion but give up. Here is what I have tried:
public static IEnumerable<PropertyInfo> GetPropertiesRecursive(this Type type)
{
var visitedProps= new HashSet<string>();
IList<PropertyInfo> propertyInfos = new List<PropertyInfo>();
var currentTypeInfo = type.GetTypeInfo();
while (currentTypeInfo.AsType() != typeof(object))
{
var unvisitedProperties = currentTypeInfo.DeclaredProperties.Where(p => p.CanRead &&
p.GetMethod.IsPublic &&
!p.GetMethod.IsStatic &&
!visitedProps.Contains(p.Name));
foreach (var propertyInfo in unvisitedProperties)
{
visitedProps.Add(propertyInfo.Name);
propertyInfos.Add(propertyInfo);
}
currentTypeInfo = currentTypeInfo.BaseType.GetTypeInfo();
}
return propertyInfos;
}
Aucun commentaire:
Enregistrer un commentaire