/// <summary> /// 父类转子类 /// </summary> /// <typeparam name="TParent"></typeparam> /// <typeparam name="TChild"></typeparam> /// <param name="parent"></param> /// <returns></returns> public static TChild AutoCopy<TParent, TChild>(TParent parent) where TChild : TParent, new() { TChild child = new TChild(); var ParentType = typeof(TParent); var Properties = ParentType.GetProperties(); foreach (var Propertie in Properties) { //循环遍历属性 if (Propertie.CanRead && Propertie.CanWrite) { //进行属性拷贝 Propertie.SetValue(child, Propertie.GetValue(parent, null), null); } } return child; } /// <summary> /// 类型转换 /// </summary> /// <typeparam name="T"></typeparam> /// <typeparam name="R"></typeparam> /// <param name="obj"></param> /// <returns></returns> public static R ToResult<T, R>(T obj) where R : new() { R t = new R(); var props = typeof(T).GetProperties().Where(a => a.CanRead && a.CanWrite); foreach (var item in props) { item.SetValue(t, item.GetValue(obj, null), null); } return t; } /// <summary> /// 检查对象中的属性是有有null值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="t"></param> /// <returns></returns> public static bool CheckObjHaveNullFiled<T>(T t) { if (t == null) { return true; } else { //属性列表 var proList = typeof(T).GetProperties(); return proList.Where(a => a.GetValue(t, null) == null || string.IsNullOrEmpty(a.GetValue(t, null).ToString().Trim())).Count() > 0; } }