C# list 分片,对象深拷贝

        /// <summary>
        /// list分片
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="blockSize"></param>
        /// <returns></returns>
        public static List<List<T>> GetBlockList<T>(List<T> list, int blockSize = 5)
        {
            List<List<T>> result = new List<List<T>>();
            var temp = new List<T>();
            for (int i = 0; i < list.Count; i++)
            {
                temp.Add(list[i]);
                if ((i + 1) % blockSize == 0 && i > 0)
                {
                    result.Add(temp);
                    temp = new List<T>();
                }
                if (i == list.Count - 1)
                {
                    result.Add(temp);
                }
            }
            return result;
        }
//className  为类的名称
        public className DeepClone()
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xml = new XmlSerializer(typeof(className));
                xml.Serialize(ms, this);
                ms.Seek(0, SeekOrigin.Begin);
                retval = xml.Deserialize(ms);
                ms.Close();
            }
            return (className)retval;
        }

  

posted @ 2022-09-03 21:06  互联网CV工程师  阅读(179)  评论(0编辑  收藏  举报