private void UpdateMarket<TSource, TTarget>(TSource s, TTarget t, string[] reqItems = null, bool create = false) where TSource : new()
where TTarget : new()
{
foreach (var p in t.GetType().GetProperties()) // 以目標(biāo)表為參照對象
{
// 復(fù)制到本地變量
var p1 = p;
// PkId 字段略過,Insert時(shí)略過Update,Update時(shí)略過Create。
if (p1.Name.CompareExt("PkId") || (create ? p1.Name.CompareExt("DateTimeUpdate") : p1.Name.CompareExt("DateTimeCreate"))) continue;
if (reqItems != null && reqItems.All(w => w != p1.Name)) continue;
var s1 = s.GetType().GetField(p1.Name); // 找到來源實(shí)體類的字段信息
if (s1 != null)
{
p1.SetValue(t, s1.GetValue(null), null);
}
else
{
var s2 = s.GetType().GetProperty(p1.Name);
if (s2 == null) continue;
p1.SetValue(t, s2.GetValue(s, null), null);
}
}
}
|