首页 > 其他 > 详细

List自增Identity列

时间:2015-03-23 19:28:35      阅读:302      评论:0      收藏:0      [点我收藏+]
/// <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;
        }
    }


 

List自增Identity列

原文:http://www.cnblogs.com/JJ.Net/p/4360598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!