Skip to content

Instantly share code, notes, and snippets.

@DevJohnC
Created December 29, 2011 02:01
Show Gist options
  • Select an option

  • Save DevJohnC/1531138 to your computer and use it in GitHub Desktop.

Select an option

Save DevJohnC/1531138 to your computer and use it in GitHub Desktop.
public class ModelTest : DynamicModel
{
public ModelTest()
: base("pgtest", "\"ModelTest\"", "id")
{
}
}
public class ModelTestRecord
{
public int id { get; set; }
public string test1 { get; set; }
public string test2 { get; set; }
}
public static class ExpandoCasting
{
public static T To<T>(this System.Dynamic.ExpandoObject record) where T : new()
{
var dictionary = record.ToDictionary();
T obj = new T();
var objType = obj.GetType();
var properties = objType.GetProperties();
foreach (var property in properties)
{
if (dictionary.ContainsKey(property.Name))
{
var fieldval = dictionary[property.Name];
property.SetValue(obj, fieldval, null);
}
}
return obj;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment