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);

            }
        }

 

posted @ 2012-04-12 10:17  ronphy  阅读(1002)  评论(1)    收藏  举报