關於對象轉換已經有不少輪子(AutoMapper,TinyMapper) .出於項目需要,手動造一個簡單輪子。先貼代碼 g>1.採用靜態泛型類緩存,避免了拆箱裝箱操作。 2.對於轉換對象中有,欄位名一樣但是類型不一樣的類時仍可以用 1 public static class Mapper<TSour ...
關於對象轉換已經有不少輪子(AutoMapper,TinyMapper) .出於項目需要,手動造一個簡單輪子。先貼代碼
g>1.採用靜態泛型類緩存,避免了拆箱裝箱操作。
2.對於轉換對象中有,欄位名一樣但是類型不一樣的類時仍可以用
1 public static class Mapper<TSource, TTarget> where TSource : class where TTarget : class 2 { 3 public readonly static Func<TSource, TTarget> Map; 4 5 static Mapper() 6 { 7 if (Map == null) 8 Map = GetMap(); 9 } 10 11 private static Func<TSource, TTarget> GetMap() 12 { 13 var sourceType = typeof(TSource); 14 var targetType = typeof(TTarget); 15 16 var parameterExpression = Expression.Parameter(sourceType, "p"); 17 var memberInitExpression = GetExpression(parameterExpression, sourceType, targetType); 18 19 var lambda = Expression.Lambda<Func<TSource, TTarget>>(memberInitExpression, parameterExpression); 20 return lambda.Compile(); 21 } 22 23 /// <summary> 24 /// 根據轉換源和目標獲取表達式樹 25 /// </summary> 26 /// <param name="parameterExpression">表達式參數p</param> 27 /// <param name="sourceType">轉換源類型</param> 28 /// <param name="targetType">轉換目標類型</param> 29 /// <returns></returns> 30 private static MemberInitExpression GetExpression(Expression parameterExpression, Type sourceType, Type targetType) 31 { 32 var memberBindings = new List<MemberBinding>(); 33 foreach (var targetItem in targetType.GetProperties().Where(x => x.PropertyType.IsPublic && x.CanWrite)) 34 { 35 var sourceItem = sourceType.GetProperty(targetItem.Name); 36 37 //判斷實體的讀寫許可權 38 if (sourceItem == null || !sourceItem.CanRead || sourceItem.PropertyType.IsNotPublic) 39 continue; 40 41 //標註NotMapped特性的屬性忽略轉換 42 if (sourceItem.GetCustomAttribute<NotMappedAttribute>() != null) 43 continue; 44 45 var propertyExpression = Expression.Property(parameterExpression, sourceItem); 46 47 //判斷都是class 且類型不相同時 48 if (targetItem.PropertyType.IsClass && sourceItem.PropertyType.IsClass && targetItem.PropertyType != sourceItem.PropertyType) 49 { 50 if (targetItem.PropertyType != targetType)//防止出現自己引用自己無限遞歸 51 { 52 var memberInit = GetExpression(propertyExpression, sourceItem.PropertyType, targetItem.PropertyType); 53 memberBindings.Add(Expression.Bind(targetItem, memberInit)); 54 continue; 55 } 56 } 57 58 if (targetItem.PropertyType != sourceItem.PropertyType) 59 continue; 60 61 memberBindings.Add(Expression.Bind(targetItem, propertyExpression)); 62 } 63 return Expression.MemberInit(Expression.New(targetType), memberBindings); 64 } 65 }View Code
3.調用方法如下
(1)構造樣例類
public class A { public int Id { get; set; } public string Name { get; set; } public C User { get; set; } /// <summary> /// 標註為notmapped特性時,不轉換賦值 /// </summary> [System.ComponentModel.DataAnnotations.Schema.NotMapped] public D UserA { get; set; } } public class B { public int Id { get; set; } public string Name { get; set; } public D User { get; set; }
public D UserA { get; set; } } public class C { public int Id { get; set; } public string Name { get; set; } } public class D { public int Id { get; set; } public string Name { get; set; } }
(2) 調用
var a = new A { Id = 1, Name = "張三", User = new C { Id = 1, Name = "李四" } };
B b = Mapper<A, B>.Map(a);//得到轉換結果
4.性能測試
1 var length = 10000000; 2 var listA = new List<A>(); 3 for (int i = 0; i < length; i++) 4 { 5 listA.Add(new A 6 { 7 Id = i, 8 Name = "張三", 9 User = new C 10 { 11 Id = i, 12 Name = "李四" 13 } 14 }); 15 } 16 17 var sw = Stopwatch.StartNew(); 18 for (int i = 0; i < length; i++) 19 { 20 var item = listA[i]; 21 var b = new B 22 { 23 Id = item.Id, 24 Name = item.Name, 25 User = new D 26 { 27 Id = i, 28 Name = "李四", 29 } 30 }; 31 } 32 sw.Stop(); 33 Console.WriteLine($"原生的時間:{sw.ElapsedMilliseconds}ms"); 34 35 //表達式 36 Mapper<A, B>.Map(listA[0]);//預先編譯緩存 37 sw.Restart(); 38 for (int i = 0; i < length; i++) 39 { 40 Mapper<A, B>.Map(listA[i]); 41 } 42 sw.Stop(); 43 Console.WriteLine($"表達式的時間:{sw.ElapsedMilliseconds}ms"); 44 45 //AutoMapper 46 AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<A, B>()); 47 sw.Restart(); 48 for (int i = 0; i < length; i++) 49 { 50 var b = AutoMapper.Mapper.Map<B>(listA[i]); 51 } 52 sw.Stop(); 53 Console.WriteLine($"AutoMapper時間:{sw.ElapsedMilliseconds}ms"); 54 55 //TinyMapper 56 TinyMapper.Bind<A, B>(); 57 sw.Restart(); 58 for (int i = 0; i < length; i++) 59 { 60 var b = TinyMapper.Map<B>(listA[i]); 61 } 62 sw.Stop(); 63 Console.WriteLine($"TinyMapper時間:{sw.ElapsedMilliseconds}ms"); 64 65 Console.ReadLine();View Code
5. 1000萬數據不帶子類集結果
6. 1000萬數據帶子類集結果