Skip to content

Instantly share code, notes, and snippets.

@Kukks
Created November 3, 2016 11:09
Show Gist options
  • Save Kukks/c98f6f2a2321e1eb4a7d92b5bfda9407 to your computer and use it in GitHub Desktop.
Save Kukks/c98f6f2a2321e1eb4a7d92b5bfda9407 to your computer and use it in GitHub Desktop.
C# helper method to dig through an object recursively via string
private readonly BindingFlags flags = BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private object GetPropertyRecursive(string property, object obj)
{
var splitted = property.Split('.');
var value = obj.GetType().GetProperty(splitted[0], flags).GetValue(obj);
if (value == null)
{
return null;
}
if (splitted.Length == 1)
{
return value;
}
return GetPropertyRecursive(string.Join(".", splitted.Skip(1)), value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment