• C# DataTable 和List之间相互转换的方法


    http://www.cnblogs.com/YoungPop-Chen/p/3259537.html#undefined

     public class TypeConversion
        {
            #region list<T>转datatable
            public static DataTable ToDataTable<T>(List<T> items)
            {
                var tb = new DataTable(typeof(T).Name);
    
                PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    
                foreach (PropertyInfo prop in props)
                {
                    Type t = GetCoreType(prop.PropertyType);
                    tb.Columns.Add(prop.Name, t);
                }
    
                foreach (T item in items)
                {
                    var values = new object[props.Length];
    
                    for (int i = 0; i < props.Length; i++)
                    {
                        values[i] = props[i].GetValue(item, null);
                    }
    
                    tb.Rows.Add(values);
                }
    
                return tb;
            }
            public static Type GetCoreType(Type t)
            {
                if (t != null && IsNullable(t))
                {
                    if (!t.IsValueType)
                    {
                        return t;
                    }
                    else
                    {
                        return Nullable.GetUnderlyingType(t);
                    }
                }
                else
                {
                    return t;
                }
            }
            public static bool IsNullable(Type t)
            {
                return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
            }
            #endregion
    
            # region DataTable转换到List
            public static IList<T> ConvertTo<T>(DataTable table)
            {
                if (table == null)
                {
                    return null;
                }
    
                List<DataRow> rows = new List<DataRow>();
    
                foreach (DataRow row in table.Rows)
                {
                    rows.Add(row);
                }
    
                return ConvertTo<T>(rows);
            }
            public static IList<T> ConvertTo<T>(IList<DataRow> rows)
            {
                IList<T> list = null;
    
                if (rows != null)
                {
                    list = new List<T>();
    
                    foreach (DataRow row in rows)
                    {
                        T item = CreateItem<T>(row);
                        list.Add(item);
                    }
                }
    
                return list;
            }
            public static T CreateItem<T>(DataRow row)
            {
                T obj = default(T);
                if (row != null)
                {
                    obj = Activator.CreateInstance<T>();
    
                    foreach (DataColumn column in row.Table.Columns)
                    {
                        PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                        try
                        {
                            object value = row[column.ColumnName];
                            prop.SetValue(obj, value, null);
                        }
                        catch
                        {  //You can log something here     
                           //throw;    
                        }
                    }
                }
    
                return obj;
            }
            #endregion
        }
  • 相关阅读:
    跨域解决方法
    css之line-height
    untiy项目中使用MD5加密
    unity给子物体添加Shader
    unity中UI坐标转3d世界坐标
    unity项目字符串转为Vector3和Quaternion
    unity中使用Highlighting System v4.0插件给物体添加高亮
    加载AssetBundle方法
    Lua面向对象----类、继承、多继承、单例的实现
    Lua学习笔记(一)-----C#和lua的交互
  • 原文地址:https://www.cnblogs.com/xinbaba/p/7803092.html
Copyright © 2020-2023  润新知