当前位置:网站首页>如果通过 C# 实现对象的深复制 ?
如果通过 C# 实现对象的深复制 ?
2022-04-23 03:02:00 【dotNET跨平台】
咨询区
NakedBrunch
我想实现 引用类型对象 之间的深复制,也就是在新的对象上修改不会影响到老的对象,我用了 C# 提供的 Clone 方法。
MyObject myObj = GetMyObj(); // Create and fill a new object
MyObject newObj = myObj.Clone();
但貌似这样行不通,请问该如何正确实现?
回答区
craastad
如果你的引用类型对象是 json 友好的,那你完全可以借助第三方工具将其转为 Json,比如 Json.NET ,参考如下的 Clone 扩展方法。
public static T Clone<T>(this T theObject)
{
string jsonData = JsonConvert.SerializeObject(theObject);
return JsonConvert.DeserializeObject<T>(jsonData);
}
然后像下面这样使用。
NewObject = OldObject.Clone();
Stacked
如果你要快速实现,建议使用对象映射化工具 AutoMapper, 它是一个高效并且轻量级的将一个对象转为另一个对象,它的底层使用的是 Lambda 表达式树,参考如下代码:
MyType source = new MyType();
Mapper.CreateMap<MyType, MyType>();
MyType target = Mapper.Map<MyType, MyType>(source);
上面的target就是深copy后的对象,如果你觉得这样还繁琐的化,再封装到一个扩展方法中去。
public static T Copy<T>(this T source)
{
T copy = default(T);
Mapper.CreateMap<T, T>();
copy = Mapper.Map<T, T>(source);
return copy;
}
然后像下面这样使用。
MyType copy = source.Copy();
Marcell Toth
通常来说,做深层的copy,方法有如下几种。
序列化
本质上来说,序列化是非常慢的一种方式,而且还限制重重,比如说:
BinaryFormatter 需要引用类型必须实现
Serializable特性。JsonConverter 需要引用类型必须有无参构造函数。
表达式树
要先加速,可以使用 Expression Tree 或者 Reflection.Emit 来动态生成深复制代码,但这种原始的方式实现起来特别麻烦,我为此专门写了一个映射工具,参见 github:https://github.com/marcelltoth/ObjectCloner
实现起来非常方便,参考如下代码:
var clone = ObjectCloner.DeepClone(original);
我的方法 ~3x 于 Reflection, ~12x 于 Newtonsoft.Json 。
点评区
其实 Dapper 用的就是 Emit 来实现高速映射, AutoMapper 用的是 Expresstion Tree 实现高速映射,大家有兴趣可以了解下。
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124239623
边栏推荐
- Navicat failed to connect to Oracle Database: cannot load OCI DLL, 87: instant client package is
- Innovation and management based on Scrum
- Kubernetes - Introduction to actual combat
- Introduction and use of openfeign component
- eventBus
- 微软是如何解决 PC 端程序多开问题的
- Error installing Mongo service 'mongodb server' on win10 failed to start
- MySQL insert free column
- Summary of interface automation interview questions for software testing
- What is the difference between varchar and char?
猜你喜欢

MAUI初体验:爽

tf. keras. layers. Timedistributed function

Leangoo brain map - shared multi person collaborative mind mapping tool

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)

Deep q-network (dqn)

AspNetCore配置多环境log4net配置文件

Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang

Small companies don't make formal offers

HLS / chisel practice CORDIC high performance computing complex square root

Array and collection types passed by openfeign parameters
随机推荐
The space between the left and right of the movie ticket seats is empty and cannot be selected
Basic workflow of CPU
《信息系统项目管理师总结》第五章 项目质量管理
Service avalanche effect
tf. keras. layers. MaxPooling? D function
Use of MySQL command line client and common commands
Huawei machine test question -- deformation of hj53 Yang Hui triangle
tf. keras. layers. Timedistributed function
Reverse a linked list < difficulty coefficient >
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Shell script learning -- practical case
The usage of case when and select case when is very easy to use
Regular object type conversion tool - Common DOM class
Array and collection types passed by openfeign parameters
Chapter IV project cost management of information system project manager summary
The difference between encodeuri and encodeuricomponent
Encapsulation of ele table
JS relearning
Introduction to ACM [inclusion exclusion theorem]
AC & A2C & A3C