C# 利用反射实现深拷贝

//利用反射实现深拷贝
        public static T DeepCopyByReflection<T>(this T tSource)
        {
            T tResult = Activator.CreateInstance<T>();
            Type sourceType = typeof(T);
            Type resultType = typeof(T);
            var sourcePros = sourceType.GetProperties();
            foreach (var pro in sourcePros)
            {
                var sourceProValue = pro.GetValue(tSource);
                var resultPro = resultType.GetProperty(pro.Name);
                resultPro.SetValue(tResult, sourceProValue);
            }
            return tResult;
        }

  

Person p1 = new PPerson p2 = p1.DeepCopyByReflection();erson { Id = 1, Name = "wjire" };

  

上一篇:Mac终端命令失效( command not found)/


下一篇:CheckBook Pro for mac(个人理财管理工具)v2.6.22免激活