You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSWeb.Common.Helper
{
public class ModelCopyHelper
{
public static object CopyModel(object src)
{
var type = src.GetType();
var objNew = Activator.CreateInstance(type);
var propList = type.GetProperties();
foreach (var prop in propList)
{
var val = prop.GetValue(src);
prop.SetValue(objNew, val);
}
return objNew;
}
public static void CopyModel(object src, object des)
{
var type = src.GetType();
var typeDes = des.GetType();
if (!type.Equals(typeDes))
{
throw new Exception("两个对象类型不一致");
}
var propList = type.GetProperties();
foreach (var prop in propList)
{
var val = prop.GetValue(src);
prop.SetValue(des, val);
}
}
}
}