DTO的深度克隆实现

               

 /// <summary>
        
/// 克隆方法
        
/// </summary>
        
/// <returns></returns>
        public object Clone()
        {
            try
            {

                Type type = this.GetType();

                VoucherDTO vd = (VoucherDTO)Activator.CreateInstance(type);

                PropertyInfo[] pilist = type.GetProperties();

                foreach (PropertyInfo item in pilist)
                {
                    PropertyInfo pi = this.GetType().GetProperty(item.Name);

                    if (pi != null)
                    {
                        object value = pi.GetValue(thisnull);

                        item.SetValue(vd, value, null);
                    }
                }

                vd.Details = new List<VoucherDTO>();

                return vd;
            }
            catch (Exception ex)
            {
                throw new Exception("克隆方法出现异常!", ex);

            }
        }
原文地址:https://www.cnblogs.com/ronphy/p/2443460.html