/// <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; } }