这期内容当中小编将会给大家带来有关如何在.NET core中对对象进行转换,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
为海门等地区用户提供了全套网页设计制作服务,及海门网站建设行业解决方案。主营业务为网站建设、成都网站设计、海门网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!示例代码
g>1.采用静态泛型类缓存,避免了拆箱装箱操作。
2.对于转换对象中有,字段名一样但是类型不一样的类时仍可以用
public static class Mapperwhere TSource : class where TTarget : class { public readonly static Func Map; static Mapper() { if (Map == null) Map = GetMap(); } private static Func 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 >(memberInitExpression, parameterExpression); return lambda.Compile(); } /// /// 根据转换源和目标获取表达式树 /// /// 表达式参数p /// 转换源类型 /// 转换目标类型 ///private static MemberInitExpression GetExpression(Expression parameterExpression, Type sourceType, Type targetType) { var memberBindings = new List (); 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 () != 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; } ////// 标注为notmapped特性时,不转换赋值 /// [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.Map(a);//得到转换结果
4.性能测试
var length = 10000000; var listA = new List(); 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.Map(listA[0]);//预先编译缓存 sw.Restart(); for (int i = 0; i < length; i++) { Mapper.Map(listA[i]); } sw.Stop(); Console.WriteLine($"表达式的时间:{sw.ElapsedMilliseconds}ms"); //AutoMapper AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap()); sw.Restart(); for (int i = 0; i < length; i++) { var b = AutoMapper.Mapper.Map(listA[i]); } sw.Stop(); Console.WriteLine($"AutoMapper时间:{sw.ElapsedMilliseconds}ms"); //TinyMapper TinyMapper.Bind(); sw.Restart(); for (int i = 0; i < length; i++) { var b = TinyMapper.Map(listA[i]); } sw.Stop(); Console.WriteLine($"TinyMapper时间:{sw.ElapsedMilliseconds}ms"); Console.ReadLine();
5. 1000万数据不带子类集结果
6. 1000万数据带子类集结果
上述就是小编为大家分享的如何在.NET core中对对象进行转换了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。