Example #1
0
        public static T CreateObject <T>(IDataReader dr) where T : new()
        {
            if (typeof(T) == typeof(DynamicObj) || typeof(T) == typeof(Dictionary <string, object>))
            {
                return((T)(object)DynamicObj.ToDynamic(dr));
            }


            T    entity = new T();
            Type t      = typeof(T);

            for (int i = 0; i < dr.FieldCount; i++)
            {
                string       fn = dr.GetName(i);
                PropertyInfo p  = t.GetProperty(fn, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetField);
                if (p != null && p.CanWrite)
                {
                    object v = dr.GetValue(i);
                    p.SetValue(entity, v.GetObjectValue(p.PropertyType), null);
                }
            }
            return(entity);
        }