zoukankan      html  css  js  c++  java
  • DataTable 转 Entity

    代码
    /// <summary>
    /// DataTransfer 的摘要说明
    /// </summary>
    public static class DataTransfer
    {
        
    /// <summary>
        
    /// datatable to list
        
    /// </summary>
        
    /// <typeparam name="T"></typeparam>
        
    /// <param name="dt"></param>
        
    /// <returns></returns>
        public static List<T> ToList<T>(DataTable dt)
        {
            List
    <T> list = new List<T>();
            
    if (dt == nullreturn list;
            
    if (dt.Rows.Count < 1return 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;
        }

        
    /// <summary>
        
    /// builder
        
    /// </summary>
        
    /// <typeparam name="T"></typeparam>
        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;
            }
        }
  • 相关阅读:
    【技术贴】解决Eclipse编译java源文件之后没有生成class文件|找不到class文件
    小米1s 正式为我服役,纪念一下。
    解决servlet的out输出流html中文乱码
    小米1s充电时屏幕一直亮着不关闭的解决办法
    今天遇到的问题分析
    java操作Excel(org.apache.poi.hssf.usermodel)
    HTML滚动文字代码 marquee标签
    MIUI小米 卸载金山安全服务
    Oracle DBA 逻辑备份试题
    Oracle DBA结构试题1
  • 原文地址:https://www.cnblogs.com/OSoft/p/1895485.html
Copyright © 2011-2022 走看看