Exemple #1
0
        /// <summary>
        /// 将DataTable转化成List对象
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static List <T> ConvertToList(DataTable dt)
        {
            List <T>                    list = new List <T>();
            DynamicBuilder <T>          builder;
            Dictionary <string, string> columns = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

#if CheckColumnExists
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string name = dt.Columns[i].ColumnName;
                if (!columns.ContainsKey(name))
                {
                    columns.Add(name, name);
                }
            }
#endif
            if (dictBuilderDt.ContainsKey(typeof(T)))
            {
                builder = dictBuilderDt[typeof(T)];
            }
            else
            {
                builder = DynamicBuilder <T> .CreateBuilder(dt.NewRow());

                dictBuilderDt.Add(typeof(T), builder);
            }

            foreach (DataRow dr in dt.Rows)
            {
                list.Add(builder.BuildT(dr, columns));
            }

            return(list);
        }
Exemple #2
0
        /// <summary>
        /// 把IDataReader转化为List对象列表
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static List <T> ConvertToList(IDataReader reader)
        {
            List <T>                    list = new List <T>();
            DynamicBuilder <T>          builder;
            Dictionary <string, string> columns = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);

#if CheckColumnExists
            for (int i = 0; i < reader.FieldCount; i++)
            {
                string name = reader.GetName(i);
                if (!columns.ContainsKey(name))
                {
                    columns.Add(name, name);
                }
            }
#endif
            if (dictBuilder.ContainsKey(typeof(T)))
            {
                builder = dictBuilder[typeof(T)];
            }
            else
            {
                builder = DynamicBuilder <T> .CreateBuilder(reader);

                lock (dictBuilder)
                {
                    if (!dictBuilder.ContainsKey(typeof(T)))
                    {
                        dictBuilder.Add(typeof(T), builder);
                    }
                }
            }

            while (reader.Read())
            {
                list.Add(builder.Build(reader, columns));
            }

            return(list);
        }
Exemple #3
0
        /// <summary>
        /// 创建 DataRow
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        private static DynamicBuilder <T> CreateBuilder(DataRow dataRecord)
        {
            DynamicBuilder <T> dynamicBuilder = new DynamicBuilder <T>();
            DynamicMethod      method         = new DynamicMethod("DynamicCreateEntity", typeof(T), new Type[] { typeof(DataRow), typeof(Dictionary <string, string>) }, typeof(T), true);
            ILGenerator        generator      = method.GetILGenerator();
            LocalBuilder       result         = generator.DeclareLocal(typeof(T));

            generator.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
            generator.Emit(OpCodes.Stloc, result);

            PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            foreach (var prop in props)
            {
#if CheckColumnExists
                //if(dictionary.ContainsKey(prop.Name){
                Label endIfLabel = generator.DefineLabel();
                generator.Emit(OpCodes.Ldarg_1);
                generator.Emit(OpCodes.Ldstr, prop.Name);
                generator.Emit(OpCodes.Callvirt, hasColumnMethod);
                generator.Emit(OpCodes.Brfalse, endIfLabel);
#endif

                //if(TypeOf.IsDBNull(iDataRecord,prop.Name){
                Label endIfLabel2 = generator.DefineLabel();
                generator.Emit(OpCodes.Ldarg_0);
                generator.Emit(OpCodes.Ldstr, prop.Name);
                generator.Emit(OpCodes.Call, isDBNullMethodDT);
                generator.Emit(OpCodes.Brtrue, endIfLabel2);


                // Depending on the type of the property, convert the datareader column value to the type
                switch (prop.PropertyType.Name)
                {
                case "Int16":
                case "Int32":
                case "Int64":
                case "UInt16":
                case "UInt32":
                case "UInt64":
                case "Byte":
                case "SByte":
                case "Boolean":
                case "String":
                case "DateTime":
                case "Single":
                case "Double":
                case "Decimal":
                case "Char":
                    // Load the T instance, SqlDataReader parameter and the field name onto the stack
                    generator.Emit(OpCodes.Ldloc_0);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.Emit(OpCodes.Ldstr, prop.Name);

                    // Push the column value onto the stack
                    generator.Emit(OpCodes.Callvirt, getValueMethodDT);

                    //Convert Common Type
                    generator.Emit(OpCodes.Call, typeof(Convert).GetMethod("To" + prop.PropertyType.Name, new Type[] { typeof(object) }));
                    break;

                case "Nullable`1":
                    // Load the T instance, SqlDataReader parameter and the field name onto the stack
                    generator.Emit(OpCodes.Ldloc_0);
                    generator.Emit(OpCodes.Ldarg_0);
                    generator.Emit(OpCodes.Ldstr, prop.Name);

                    // Push the column value onto the stack
                    generator.Emit(OpCodes.Callvirt, getValueMethodDT);

                    //Convert Nullable Type
                    generator.Emit(OpCodes.Unbox_Any, prop.PropertyType);
                    break;

                default:
#if CheckColumnExists
                    generator.MarkLabel(endIfLabel);
                    //}
#endif
                    generator.MarkLabel(endIfLabel2);
                    // Don't set the field value as it's an unsupported type
                    continue;
                }

                generator.Emit(OpCodes.Callvirt, prop.GetSetMethod());
                //}
                generator.MarkLabel(endIfLabel2);
#if CheckColumnExists
                generator.MarkLabel(endIfLabel);
                //}
#endif
            }
            generator.Emit(OpCodes.Ldloc, result);
            generator.Emit(OpCodes.Ret);

            dynamicBuilder.handlerT = (LoadT)method.CreateDelegate(typeof(LoadT));
            return(dynamicBuilder);
        }