您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關如何在.NET core中對對象進行轉換,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
示例代碼
g>1.采用靜態泛型類緩存,避免了拆箱裝箱操作。
2.對于轉換對象中有,字段名一樣但是類型不一樣的類時仍可以用
public static class Mapper<TSource, TTarget> where TSource : class where TTarget : class { public readonly static Func<TSource, TTarget> Map; static Mapper() { if (Map == null) Map = GetMap(); } private static Func<TSource, TTarget> GetMap() { var sourceType = typeof(TSource); var targetType = typeof(TTarget); var parameterExpression = Expression.Parameter(sourceType, "p"); var memberInitExpression = GetExpression(parameterExpression, sourceType, targetType); var lambda = Expression.Lambda<Func<TSource, TTarget>>(memberInitExpression, parameterExpression); return lambda.Compile(); } /// <summary> /// 根據轉換源和目標獲取表達式樹 /// </summary> /// <param name="parameterExpression">表達式參數p</param> /// <param name="sourceType">轉換源類型</param> /// <param name="targetType">轉換目標類型</param> /// <returns></returns> private static MemberInitExpression GetExpression(Expression parameterExpression, Type sourceType, Type targetType) { var memberBindings = new List<MemberBinding>(); foreach (var targetItem in targetType.GetProperties().Where(x => x.PropertyType.IsPublic && x.CanWrite)) { var sourceItem = sourceType.GetProperty(targetItem.Name); //判斷實體的讀寫權限 if (sourceItem == null || !sourceItem.CanRead || sourceItem.PropertyType.IsNotPublic) continue; //標注NotMapped特性的屬性忽略轉換 if (sourceItem.GetCustomAttribute<NotMappedAttribute>() != null) continue; var propertyExpression = Expression.Property(parameterExpression, sourceItem); //判斷都是class 且類型不相同時 if (targetItem.PropertyType.IsClass && sourceItem.PropertyType.IsClass && targetItem.PropertyType != sourceItem.PropertyType) { if (targetItem.PropertyType != targetType)//防止出現自己引用自己無限遞歸 { var memberInit = GetExpression(propertyExpression, sourceItem.PropertyType, targetItem.PropertyType); memberBindings.Add(Expression.Bind(targetItem, memberInit)); continue; } } if (targetItem.PropertyType != sourceItem.PropertyType) continue; memberBindings.Add(Expression.Bind(targetItem, propertyExpression)); } return Expression.MemberInit(Expression.New(targetType), memberBindings); } }
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; }<br data-filtered="filtered"> 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 = "李四" } };<br> B b = Mapper<A, B>.Map(a);//得到轉換結果
4.性能測試
var length = 10000000; var listA = new List<A>(); for (int i = 0; i < length; i++) { listA.Add(new A { Id = i, Name = "張三", User = new C { Id = i, Name = "李四" } }); } var sw = Stopwatch.StartNew(); for (int i = 0; i < length; i++) { var item = listA[i]; var b = new B { Id = item.Id, Name = item.Name, User = new D { Id = i, Name = "李四", } }; } sw.Stop(); Console.WriteLine($"原生的時間:{sw.ElapsedMilliseconds}ms"); //表達式 Mapper<A, B>.Map(listA[0]);//預先編譯緩存 sw.Restart(); for (int i = 0; i < length; i++) { Mapper<A, B>.Map(listA[i]); } sw.Stop(); Console.WriteLine($"表達式的時間:{sw.ElapsedMilliseconds}ms"); //AutoMapper AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<A, B>()); sw.Restart(); for (int i = 0; i < length; i++) { var b = AutoMapper.Mapper.Map<B>(listA[i]); } sw.Stop(); Console.WriteLine($"AutoMapper時間:{sw.ElapsedMilliseconds}ms"); //TinyMapper TinyMapper.Bind<A, B>(); sw.Restart(); for (int i = 0; i < length; i++) { var b = TinyMapper.Map<B>(listA[i]); } sw.Stop(); Console.WriteLine($"TinyMapper時間:{sw.ElapsedMilliseconds}ms"); Console.ReadLine();
5. 1000萬數據不帶子類集結果
6. 1000萬數據帶子類集結果
上述就是小編為大家分享的如何在.NET core中對對象進行轉換了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。