Created
November 3, 2016 11:09
-
-
Save Kukks/c98f6f2a2321e1eb4a7d92b5bfda9407 to your computer and use it in GitHub Desktop.
C# helper method to dig through an object recursively via string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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