zoukankan      html  css  js  c++  java
  • DataTableToList 优化后 武胜

    View Code http://www.codeproject.com/Articles/19513/Dynamic-But-Fast-The-Tale-of-Three-Monkeys-A-Wolf
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Reflection;
    using System.Reflection.Emit;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SharpShell
    {
        //DataTable转Entity(Emit版)
        //30000条数据,直接发射的话7秒多,Emit1.2秒左右
        public static class NewClas
        {
    
            public static List<T> ToList<T>(DataTable dt)
            {
                List<T> list = new List<T>();
                if (dt == null) return list;
                DataTableEntityBuilder<T> eblist = DataTableEntityBuilder<T>.CreateBuilder(dt.Rows[0]);
                foreach (DataRow info in dt.Rows)
                    list.Add(eblist.Build(info));
                dt.Dispose();
                dt = null;
                return list;
            }
    
            public class DataTableEntityBuilder<T>
            {
                private static readonly MethodInfo getValueMethod = typeof (DataRow).GetMethod("get_Item",
                                                                                               new Type[] {typeof (int)});
    
                private static readonly MethodInfo isDBNullMethod = typeof (DataRow).GetMethod("IsNull",
                                                                                               new Type[] {typeof (int)});
    
                private delegate T Load(DataRow dataRecord);
    
                private Load handler;
    
                private DataTableEntityBuilder()
                {
                }
    
                public T Build(DataRow dataRecord)
                {
                    return handler(dataRecord);
                }
    
    
                public static DataTableEntityBuilder<T> CreateBuilder(DataRow dataRow)
                {
                    DataTableEntityBuilder<T> dynamicBuilder = new DataTableEntityBuilder<T>();
                    DynamicMethod method = new DynamicMethod("DynamicCreateEntity", typeof (T),
                                                             new Type[] {typeof (DataRow)}, 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);
    
                    for (int index = 0; index < dataRow.ItemArray.Length; index++)
                    {
                        PropertyInfo propertyInfo = typeof (T).GetProperty(dataRow.Table.Columns[index].ColumnName);
                        Label endIfLabel = generator.DefineLabel();
                        if (propertyInfo != null && propertyInfo.GetSetMethod() != null)
                        {
                            generator.Emit(OpCodes.Ldarg_0);
                            generator.Emit(OpCodes.Ldc_I4, index);
                            generator.Emit(OpCodes.Callvirt, isDBNullMethod);
                            generator.Emit(OpCodes.Brtrue, endIfLabel);
                            generator.Emit(OpCodes.Ldloc, result);
                            generator.Emit(OpCodes.Ldarg_0);
                            generator.Emit(OpCodes.Ldc_I4, index);
                            generator.Emit(OpCodes.Callvirt, getValueMethod);
                            generator.Emit(OpCodes.Unbox_Any, propertyInfo.PropertyType);
                            generator.Emit(OpCodes.Callvirt, propertyInfo.GetSetMethod());
                            generator.MarkLabel(endIfLabel);
                        }
                    }
                    generator.Emit(OpCodes.Ldloc, result);
                    generator.Emit(OpCodes.Ret);
                    dynamicBuilder.handler = (Load) method.CreateDelegate(typeof (Load));
                    return dynamicBuilder;
                }
            }
        }
    }
  • 相关阅读:
    K-Means++ 聚类之数据可视化:使用gnuplot
    QQ设计第1-5步
    QQ设计第1-5步
    为什么有很深的windows基础还是不能动摇linux半步
    常用命令
    在线会计_金蝶友商网
    XP使用VNC远程桌面CentOS 6
    Fatal error: Call to undefined function mb_substr()
    如何汉化 po 文件及编译成 mo 文件
    idoerp
  • 原文地址:https://www.cnblogs.com/zeroone/p/3041918.html
Copyright © 2011-2022 走看看