public class ModelToDTO { ////// 根据模型对象获取扁平类对象 /// ///模型类 ///传输类 /// 模型类对象 /// 传输类对象 ///public static T GetDTOModel (S s, T t) { if (s != null) { AutoMapping(s, t); return t; } else { return default(T); } } ////// 实体属性反射 /// ///赋值对象 ///被赋值对象 /// /// private static void AutoMapping(S s, T t) { PropertyInfo[] pps = GetPropertyInfos(s.GetType()); 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); } } } //获取对象属性 private static PropertyInfo[] GetPropertyInfos(Type type) { return type.GetProperties(BindingFlags.Public | BindingFlags.Instance); } }