将一个实体的值自动赋值给另一个实体

   
今天需要用到将实体A的字段自动赋值给实体B的字段

public static T Convert<U, T>(U _u) where U : new() where T : new() { T t_ = new T(); PropertyInfo[] tpis = t_.GetType().GetProperties(); foreach (PropertyInfo tpi in tpis) { PropertyInfo upi = _u.GetType().GetProperty(tpi.Name); if (upi != null) { if (!tpi.CanWrite) continue; object value = upi.GetValue(_u, null); if (value != DBNull.Value && tpi.PropertyType == upi.PropertyType) tpi.SetValue(t_, value, null); } } return t_; }

 

posted @ 2015-07-03 11:42  feehuang  阅读(216)  评论(0)    收藏  举报