這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)如何在.NET core中對(duì)對(duì)象進(jìn)行轉(zhuǎn)換,文章內(nèi)容豐富且以專(zhuān)業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
為海門(mén)等地區(qū)用戶(hù)提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及海門(mén)網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、海門(mén)網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶(hù)提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶(hù)的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!示例代碼
g>1.采用靜態(tài)泛型類(lèi)緩存,避免了拆箱裝箱操作。
2.對(duì)于轉(zhuǎn)換對(duì)象中有,字段名一樣但是類(lèi)型不一樣的類(lèi)時(shí)仍可以用
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> /// 根據(jù)轉(zhuǎn)換源和目標(biāo)獲取表達(dá)式樹(shù) /// </summary> /// <param name="parameterExpression">表達(dá)式參數(shù)p</param> /// <param name="sourceType">轉(zhuǎn)換源類(lèi)型</param> /// <param name="targetType">轉(zhuǎn)換目標(biāo)類(lèi)型</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); //判斷實(shí)體的讀寫(xiě)權(quán)限 if (sourceItem == null || !sourceItem.CanRead || sourceItem.PropertyType.IsNotPublic) continue; //標(biāo)注NotMapped特性的屬性忽略轉(zhuǎn)換 if (sourceItem.GetCustomAttribute<NotMappedAttribute>() != null) continue; var propertyExpression = Expression.Property(parameterExpression, sourceItem); //判斷都是class 且類(lèi)型不相同時(shí) if (targetItem.PropertyType.IsClass && sourceItem.PropertyType.IsClass && targetItem.PropertyType != sourceItem.PropertyType) { if (targetItem.PropertyType != targetType)//防止出現(xiàn)自己引用自己無(wú)限遞歸 { 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.調(diào)用方法如下
(1)構(gòu)造樣例類(lèi)
public class A { public int Id { get; set; } public string Name { get; set; } public C User { get; set; } /// <summary> /// 標(biāo)注為notmapped特性時(shí),不轉(zhuǎn)換賦值 /// </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) 調(diào)用
var a = new A { Id = 1, Name = "張三", User = new C { Id = 1, Name = "李四" } };<br> B b = Mapper<A, B>.Map(a);//得到轉(zhuǎn)換結(jié)果
4.性能測(cè)試
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($"原生的時(shí)間:{sw.ElapsedMilliseconds}ms"); //表達(dá)式 Mapper<A, B>.Map(listA[0]);//預(yù)先編譯緩存 sw.Restart(); for (int i = 0; i < length; i++) { Mapper<A, B>.Map(listA[i]); } sw.Stop(); Console.WriteLine($"表達(dá)式的時(shí)間:{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時(shí)間:{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時(shí)間:{sw.ElapsedMilliseconds}ms"); Console.ReadLine();
5. 1000萬(wàn)數(shù)據(jù)不帶子類(lèi)集結(jié)果
6. 1000萬(wàn)數(shù)據(jù)帶子類(lèi)集結(jié)果
上述就是小編為大家分享的如何在.NET core中對(duì)對(duì)象進(jìn)行轉(zhuǎn)換了,如果剛好有類(lèi)似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱(chēng):如何在.NETcore中對(duì)對(duì)象進(jìn)行轉(zhuǎn)換-創(chuàng)新互聯(lián)
地址分享:http://aaarwkj.com/article18/cojcdp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、用戶(hù)體驗(yàn)、自適應(yīng)網(wǎng)站、Google、軟件開(kāi)發(fā)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
全網(wǎng)營(yíng)銷(xiāo)推廣知識(shí)