I am looking for the most efficient way of iterating through attributes within an object and checking to see if it has a custom decorator. The challenge is that my object has other objects within it that may have this custom decorator and the SUB-Objects may have objects as well with decorators.
For now the code down below is only reaching into the first layer of sub objects is there a particular way in which I can go within the loop N times efficiently?
List<PropertyInfo> allProperties = type.GetProperties().ToList();
Dictionary<string, List<string>> groupIndexes = new Dictionary<string, List<string>>();
foreach (var property in type.GetProperties())
{
var nestedProperties = property.PropertyType.GetProperties();
foreach (var nestedProperty in nestedProperties)
{
var singleNestedPropertyIndex = nestedProperty.GetCustomAttribute<SingleIndexAttribute>();
var groupNestedIndex = nestedProperty.GetCustomAttribute<GroupIndexAttribute>();
var ttlIndex = property.GetCustomAttribute<TTLIndexAttribute>();
if (singleNestedPropertyIndex != null || groupNestedIndex != null || ttlIndex != null)
{
allProperties.Add(nestedProperty);
}
}
}
Stack<T>. ImplementingBreadth-first searchorDepth-first searchcould solve your problem. Both can be done using recursion or stack.