当前位置:网站首页>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
Serializable
characteristic .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
边栏推荐
- FileNotFoundError: [Errno 2] No such file or directory
- Deep q-network (dqn)
- ASP.NET 6 中间件系列 - 条件中间件
- Response processing of openfeign
- 【鉴权/授权】自定义一个身份认证Handler
- HLS / chisel practice CORDIC high performance computing complex square root
- Plug in for vscode
- 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
- 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
- 使用split来解决“最常见的单词”问题
猜你喜欢
L2-006 树的遍历(中后序确定二叉树&层序遍历)
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
Distributed system services
Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
tf. keras. layers. Embedding function
Laravel's own paging query
利用栈的回溯来解决“文件的最长绝对路径”问题
随机推荐
准备一个月去参加ACM,是一种什么体验?
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
Face longitude:
The difference between encodeuri and encodeuricomponent
微软是如何解决 PC 端程序多开问题的
求二叉树的叶子结点个数
C#中元组对象Tuple的使用
BLDC double closed loop (speed PI + current PI) Simulink simulation model
交换二叉树中每个结点的左和右
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Simple example of using redis in PHP
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
微软是如何解决 PC 端程序多开问题的——内部实现
Source Generator实战
Close the computer port
先中二叉建树
Niuke white moon race 6 [solution]
ASP.NET和ASP.NETCore多环境配置对比