当前位置:网站首页>If the deep replication of objects is realized through C #?
If the deep replication of objects is realized through C #?
2022-04-23 03:06:00 【Dotnet cross platform】
Consultation area
NakedBrunch
I want to achieve Reference type objects Deep replication between , That is, modifying the new object will not affect the old object , I used it C# Provided Clone Method .
MyObject myObj = GetMyObj(); // Create and fill a new object
MyObject newObj = myObj.Clone();
But it doesn't seem to work , How to correctly realize ?
Answer area
craastad
If your Reference type objects yes json Amicable , Then you can turn it into... With the help of third-party tools Json, such as Json.NET , Refer to the following Clone Extension method .
public static T Clone<T>(this T theObject)
{
string jsonData = JsonConvert.SerializeObject(theObject);
return JsonConvert.DeserializeObject<T>(jsonData);
}
Then use it as follows .
NewObject = OldObject.Clone();
Stacked
If you want to achieve , It is recommended to use the object mapping tool AutoMapper, It is an efficient and lightweight way to turn one object into another , Its bottom layer uses Lambda Expression tree , Refer to the following code :
MyType source = new MyType();
Mapper.CreateMap<MyType, MyType>();
MyType target = Mapper.Map<MyType, MyType>(source);
above target It's deep copy After the object , If you think it's cumbersome , Then encapsulated in an extension method .
public static T Copy<T>(this T source)
{
T copy = default(T);
Mapper.CreateMap<T, T>();
copy = Mapper.Map<T, T>(source);
return copy;
}
Then use it as follows .
MyType copy = source.Copy();
Marcell Toth
Generally speaking , Do deep copy, The methods are as follows .
serialize
In essence , Serialization is a very slow way , And there are many restrictions , for instance :
BinaryFormatter The reference type required must implement
Serializablecharacteristic .JsonConverter The reference type must have a parameterless constructor .
Expression tree
Speed up first , have access to Expression Tree perhaps Reflection.Emit To dynamically generate deep copy code , But this primitive approach is particularly troublesome , I wrote a mapping tool for this purpose , See github:https://github.com/marcelltoth/ObjectCloner
It's very convenient to implement , Refer to the following code :
var clone = ObjectCloner.DeepClone(original);
My method ~3x On Reflection, ~12x On Newtonsoft.Json .
Comment area
Actually Dapper It's used Emit To achieve high-speed mapping , AutoMapper It's using Expresstion Tree Achieve high-speed mapping , If you are interested, you can understand .
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334636.html
边栏推荐
- Blazor University (12)组件 — 组件生命周期
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
- Traversée de l'arbre L2 - 006
- Use of MySQL command line client and common commands
- Face longitude:
- 全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
- Middle and rear binary tree
- 微软是如何解决 PC 端程序多开问题的
- 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
- Q-Learning & Sarsa
猜你喜欢

ASP.NET 6 中间件系列 - 自定义中间件类
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)

再战leetcode (290.单词规律)

荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比

C#中切片语法糖的使用

L2-006 树的遍历(中后序确定二叉树&层序遍历)

tf. keras. layers. Density function

BLDC double closed loop (speed PI + current PI) Simulink simulation model

LNMP MySQL allows remote access
随机推荐
C# 11 对 ref 和 struct 的改进
Laravel's own paging query
.Net Core 限流控制-AspNetCoreRateLimit
准备一个月去参加ACM,是一种什么体验?
Blazor University (11)组件 — 替换子组件的属性
7-11 rearrange the linked list (25 points)
REINFORCE
tf. keras. layers. Density function
使用split来解决“最常见的单词”问题
Small companies don't make formal offers
Configuring Apache Web services for servers such as Tianyi cloud
Source Generator实战
Service avalanche effect
tf. keras. layers. Inputlayer function
Tips in MATLAB
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
建立与遍历二叉树
Winsock programming interface experiment: implementation of ipconfig
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
Numpy stack function