Created
October 17, 2013 19:04
-
-
Save robertaarcoverde/7030402 to your computer and use it in GitHub Desktop.
quick function for converting expression strings into objects
ex: "Account.Manager.Name", "Roberta" ==> new Account { new Manager { Name = "Roberta" } }
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 object Eval(KeyValuePair<string, object> df) | |
{ | |
var properties = df.Key.Split('.'); | |
object root = Activator.CreateInstance(Assembly.GetExecutingAssembly().GetTypes().First(t => t.Name == properties.First())); | |
var temp = root; | |
for (int i = 1; i < properties.Length - 1; i++) | |
{ | |
var propertyInfo = temp.GetType().GetProperty(properties[i]); | |
var propertyInstance = Activator.CreateInstance(propertyInfo.PropertyType); | |
propertyInfo.SetValue(temp, propertyInstance, null); | |
temp = propertyInstance; | |
} | |
temp.GetType().GetProperty(properties.Last()).SetValue(temp, df.Value, null); | |
return root; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment