C# 复制对象

  #region 两个类对拷
        public static void Mapper<T1, T2>(ref T1 to, T2 from)
        {
            foreach (var pi in from.GetType().GetProperties())
            {
                foreach (var objPi in to.GetType().GetProperties())
                {
                    if (pi.Name == objPi.Name && objPi.PropertyType ==      pi.PropertyType && pi.Name != "Id" && pi.Name != "ID")
                    {
                        var value = pi.GetValue(from, null);
                        objPi.SetValue(to, value, null);
                    }
                }
            }
        }
        #endregion

你可能感兴趣的:(C# 复制对象)