Created
December 29, 2011 02:01
-
-
Save DevJohnC/1531138 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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