I've really had a time decoding your code so to speak, are you a c++ developer by any chance?
Anyway, I think your algorithm has run-away with itself. Assuming you pass the property name like this "Property1.SubProperty2.Subproperty3", I think you need something like this:
public object FindValue(object rootObject, string properties)
{
List<string> propertyNames = new List <string>(string.Split("."));
if(string.Length == 0)
return null;
return GetValue(rootObject, propertyNames);
}
public object GetValue(object currentObject, List<string>propertyNames)
{
if(object == null)
return null;
if(propertyNames.Count == 0)
return currentObject;
PropertyInfo propertyInfo = currentObject.GetType().GetProperty(propertyNames[0]);
if(propertyInfo == null)
return null;
propertyNames.RemoveAt(0);
object nextObject= propertyInfo.GetValue(currentObject, null);
return GetValue(nextObject, propertyNames);
}
Disclaimer: I haven't tested my code & it's getting late here, this code might not be optimal, I think it makes one more iteration than it strictly needs!
propertyInfo.GetValue(currentObject, null
) only works for non-indexed values.