List自增Identity列

/// <summary>
    /// 转换实体类列表,增加自增列
    /// </summary>
    /// <typeparam name="T">原始实体类</typeparam>
    /// <typeparam name="U">目标实体类</typeparam>
    public class Class1<T,U>
    {
        /// <summary>
        /// 增加自增列
        /// </summary>
        /// <param name="list">原实体类列表</param>
        /// <param name="intIndentityColumnName">自增列名</param>
        /// <param name="initialValue">自增初始值</param>
        /// <returns></returns>
        public IList<U> AddIntIndentityColumn(IList<T> list, string intIndentityColumnName, int initialValue)
        {
            IList<U> listU = new List<U>();
            Type type = typeof(T);
            Type typeU = typeof(U);
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            int i = initialValue;
            foreach (T t in list)
            {                
                Type coltype = t.GetType();
                object object_u = Activator.CreateInstance(typeU);
                foreach (PropertyInfo p in props)
                {                    
                    object o = t.GetType().GetProperty(p.Name).GetValue(t, null);
                    object_u.GetType().GetProperty(p.Name).SetValue(object_u,o,null);
                }
                ++i;
                object_u.GetType().GetProperty(intIndentityColumnName).SetValue(object_u, i, null);
                listU.Add((U)object_u);
            }            
            return listU;
        }
    }


 

posted @ 2015-03-23 18:04  JJ.Net  阅读(378)  评论(0)    收藏  举报