Skip to content

Instantly share code, notes, and snippets.

@robertaarcoverde
Created October 17, 2013 19:04
Show Gist options
  • Save robertaarcoverde/7030402 to your computer and use it in GitHub Desktop.
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" } }
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