C# 对象映射的高性能方案
作者:乌龟会哲_Program 时间:2021-09-22 08:03:19
1.之前在使用AutoMapper 框架感觉用着比较不够灵活,而且主要通过表达式树Api 实现对象映射 ,写着比较讨厌,当出现复杂类型和嵌套类型时性能直线下降,甚至不如序列化快。
2.针对AutoMapper 处理复杂类型和嵌套类型时性能非常差的情况,自己实现一个简化版对象映射的高性能方案
public class Article
{
public int Id { get; set; }
public string CategoryId { get; set; }
public string Title { get; set; }
public string Pic { get; set; }
public string Host { get; set; }
public string PicHost => Pic.FormatHostUrl(Host);
public string Content { get; set; }
public bool TopStatus { get; set; }
public DateTime PublishDate { get; set; }
public string LastUpdateUser { get; set; }
public DateTime LastUpdateDate { get; set; }
public bool IsTeacher { get; set; }
public bool IsParent { get; set; }
public bool IsOrg { get; set; }
public bool IsLeaner { get; set; }
public string ToUserStr
{
get
{
List<string> strArr = new List<string>();
if (IsLeaner)
{
strArr.Add("学员");
}
if (IsOrg)
{
strArr.Add("机构");
}
if (IsParent)
{
strArr.Add("家长");
}
if (IsTeacher)
{
strArr.Add("老师");
}
return string.Join(",", strArr);
}
}
public int OrgId { get; set; }
public object OrgInfo { get; set; }
public string IsPlatformStr => OrgId == 0 ? "平台" : "机构";
}
现在我们来使用两行代码来搞定对象映射问题
为了实现操作更方便,多对象映射
实现对象映射功能的代码如下:
public static T CopyObjValue<T>(this T toobj, Object fromobj) where T : class
{
if (fromobj != null && toobj != null)
{
var otherobjPorps = fromobj.GetType().GetProperties();
foreach (var formp in otherobjPorps)
{
var top = toobj.GetType().GetProperty(formp.Name);
if (top != null)
{
try
{
top.SetValue(toobj, formp.GetValue(fromobj));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
return toobj;
}
来源:https://www.cnblogs.com/LeeMicroShake/p/14125166.html
标签:C#,对象映射
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
maven依赖版本没有按照最短路径原则生效的解决方案
2023-04-04 20:49:09
java中的类为什么只能用public修饰?
2023-10-09 20:23:54
MyBatis注解CRUD与执行流程深入探究
2023-07-03 06:19:44
![](https://img.aspxhome.com/file/2023/1/63011_0s.png)
android几种不同对话框的实现方式
2022-09-13 06:31:42
![](https://img.aspxhome.com/file/2023/3/98693_0s.png)
MyBatis分页插件PageHelper的使用与原理
2021-06-15 09:24:35
![](https://img.aspxhome.com/file/2023/5/101985_0s.png)
Java如何将大文件切割成小文件
2022-12-27 07:40:40
![](https://img.aspxhome.com/file/2023/5/78965_0s.png)
MyBatisPlus+Lombok实现分页功能的方法详解
2021-07-04 06:34:57
![](https://img.aspxhome.com/file/2023/3/85483_0s.png)
jstorm源码解析之bolt异常处理方法
2022-08-05 23:12:08
Java使用Gateway自定义负载均衡过滤器
2022-06-27 03:55:09
![](https://img.aspxhome.com/file/2023/8/86058_0s.jpg)
java IO流 之 输入流 InputString()的使用
2023-08-22 07:44:31
Springboot整合PageOffice 实现word在线编辑保存功能
2022-12-03 22:38:18
![](https://img.aspxhome.com/file/2023/7/88517_0s.png)
如何基于java语言实现八皇后问题
2022-08-22 09:05:25
Android Intent实现页面跳转的方法示例
2021-11-08 05:27:22
![](https://img.aspxhome.com/file/2023/9/93289_0s.png)
基于NET Core 的Nuget包制作、发布和运用流程解析(完整过程)
2022-03-10 01:09:29
![](https://img.aspxhome.com/file/2023/1/96401_0s.jpg)
log4j配置失效日志中打印Debug信息问题
2022-10-12 10:13:12
![](https://img.aspxhome.com/file/2023/7/100667_0s.png)
Java NIO实现多人聊天室
2022-04-05 09:03:17
![](https://img.aspxhome.com/file/2023/4/62964_0s.jpg)
MyBatisPlus 自定义sql语句的实现
2021-11-11 07:51:20
Java String的intern用法解析
2023-04-22 19:03:35
使用 Java8 实现观察者模式的方法(下)
2021-08-03 04:21:37
Android MediaPlayer实现音乐播放器实例代码
2021-05-26 22:58:05