using System;
using System.Data.Objects.DataClasses;
using System.Reflection;
static public void AutoMapping<S, T>(S s, T t)
{
PropertyInfo[] pps = s.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
Type target = t.GetType();
foreach (var pp in pps)
{
PropertyInfo targetPP = target.GetProperty(pp.Name);
object value = pp.GetValue(s, null);
if (targetPP != null && value != null)
{
targetPP.SetValue(t, value, null);
}
}
}
static public void CopyEFObject<S, T>(S s, T t)
{
PropertyInfo[] pps = s.GetType().GetProperties();
Type target = t.GetType();
foreach (var pp in pps)
{
PropertyInfo targetPP = target.GetProperty(pp.Name);
if (targetPP.IsDefined(typeof(EdmScalarPropertyAttribute)))
{
object value = pp.GetValue(s, null);
if (targetPP != null && value != null)
{
targetPP.SetValue(t, value, null);
}
}
}
}
CopyEFObject<Info, Info>(creditInfo, copyCredit);
// 反射循环给实体赋值
var list = new List<Abnormal>();
sourceList.ForEach(row => {
var entity = new Abnormal();
AutoMapping(row, entity);
list.Add(entity);
});
c# 反射循环给实体赋值
最新推荐文章于 2023-02-27 11:10:01 发布