您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这期内容当中小编将会给大家带来有关如何在.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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。