最近發現總會有需求,把一個實體類賦值給另一個實體類,在網上找了找,最后用反射寫的動態轉換類。但是只測試了DBModel的賦值和轉換。不多說上代碼。
/// <summary> /// 類轉類(T是接收的類型,A是傳入數據的類型) /// 注意,不可以轉換集合,轉換集合請用SetListValueByListT方法 /// 當前類用來轉換實體類對象,只是屬性和屬性的賦值轉換 /// 只轉換名字相同並且類型一致的 /// </summary> /// <typeparam name="T">轉換結果類型</typeparam> /// <typeparam name="A">要轉換的類型</typeparam> /// <param name="GetValue">傳入的參數</param> /// <returns>轉換結果</returns> public static T SetValueByT<T, A>(A GetValue) { var SetValue = System.Activator.CreateInstance<T>(); try { var TProps = SetValue.GetType().GetProperties(); var TPropsNames = TProps.ToDictionary(i => i.Name, i => i.GetType()); var TPropsTypes = TProps.Select(i => i.GetType()).ToList(); var AProps = GetValue.GetType().GetProperties(); List<PropertyInfo> Props = new List<PropertyInfo>(); foreach (var item in AProps) { if (TPropsNames.Keys.Contains(item.Name)) { if (TPropsNames[item.Name] == item.GetType()) { var Value = GetValue.GetType().GetProperty(item.Name).GetValue(GetValue); SetValue.GetType().GetProperty(item.Name).SetValue(SetValue, Value); } } } return SetValue; } catch { return SetValue; } } /// <summary> /// 集合轉換(T是接收的集合類型,A是傳入數據的集合類型) /// 當前類用來轉換實體類對象,只是屬性和屬性的賦值轉換 /// 只轉換名字相同並且類型一致的 /// </summary> /// <typeparam name="T">轉換結果類型</typeparam> /// <typeparam name="A">要轉換的類型</typeparam> /// <param name="GetListValue">傳入的集合</param> /// <returns>轉換結果</returns> public static List<T> SetListValueByListT<T, A>(List<A> GetListValue) { List<T> SetListValue = new List<T>(); if (GetListValue.Count() > 0) { var SetDefaultValue = System.Activator.CreateInstance<T>(); var TProps = SetDefaultValue.GetType().GetProperties(); var TPropsNames = TProps.ToDictionary(i => i.Name, i => i.GetType()); var TPropsTypes = TProps.Select(i => i.GetType()).ToList(); var AProps = GetListValue.First().GetType().GetProperties(); Dictionary<string, List<object>> list = new Dictionary<string, List<object>>(); foreach (var item in AProps) { if (TPropsNames.Keys.Contains(item.Name)) { if (TPropsNames[item.Name] == item.GetType()) { List<object> getPropList = GetListValue.Select(i => i.GetType().GetProperty(item.Name).GetValue(i)).ToList(); list.Add(item.Name, getPropList); } } } if (list.Keys.Count > 0) { var Count = list.ElementAt(0).Value.Count; for (int i = 0; i < Count; i++) { var NewValue = System.Activator.CreateInstance<T>(); foreach (var item in list) { NewValue.GetType().GetProperty(item.Key).SetValue(NewValue, item.Value.ElementAt(i)); } SetListValue.Add(NewValue); } } } return SetListValue; }
測試一下是否成功
先創建兩個測試類

public class Class1 { public string Name { get; set; } public string ID { get; set; } //這里名稱不一樣 public string Info { get; set; } public int Value1 { get; set; } public int Value2 { get; set; } public int Value3 { get; set; } } public class Class2 { public string Name { get; set; } public string ID { get; set; } //這里名稱不一樣 public string Infomation { get; set; } public int Value1 { get; set; } public int Value2 { get; set; } public int Value3 { get; set; } }
測試一下類轉類的結果
Class2 class2 = new Class2 { ID = "1213", Name = "name", Infomation = "測試", Value1 = 1, Value2 = 2, Value3 = 3 }; Class1 class1 = SetValueByT<Class1, Class2>(class2);
在測試一下集合和集合的轉換,插入1萬條數據
List<Class2> class2s = new List<Class2>(); for (int i = 0; i < 10000; i++) { class2s.Add(new Class2 { ID = i.ToString(), Name = "name", Infomation = "測試", Value1 = i + 1, Value2 = i + 2, Value3 = 1 + 3 }); } List<Class1> class1s = SetListValueByListT<Class1, Class2>(class2s);
成功了!
測試一下耗時一萬條數據用了134毫秒
轉載請注明原文鏈接