【转载】对象克隆(C# 快速高效率复制对象另一种方式 表达式树转)
Linqpad 代码
Benmarker测试结果耗时改善很明显,而且还可以支持深度复制,看来果然很优秀
// * Summary *
BenchmarkDotNet=v0.12.0, OS=Windows 10.0.18362
Intel Core i7-9700 CPU 3.00GHz, 1 CPU, 8 logical and 8 physical cores
.NET Core SDK=3.1.201
[Host] : .NET Core 3.1.3 (CoreCLR 4.700.20.11803, CoreFX 4.700.20.12001), X64 RyuJIT
| Method | Mean | Error | StdDev |
|
-------------------- |---------------:|--------------:|------------:|
| TestTransReflection | 1,204.881 ns | 23.2975 ns | 23.9248 ns || TestExpression | 122,195.921 ns | 1,007.6728 ns | 942.5777 ns |
| TestTransExp | 250.915 ns | 2.1132 ns | 1.9767 ns |
| TestTransExpV2 | 9.475 ns | 0.2525 ns | 0.2362 ns |
| TestAutoMappe | 176.756 ns | 3.5712 ns | 3.6673 ns |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | <Query Kind= "Program" > <Reference Relative= "..\..\Libs\AutoMapper.9.0.0\lib\netstandard2.0\AutoMapper.dll" >D:\projects\github\sui84\linqpad\LINQPad Queries\Libs\AutoMapper.9.0.0\lib\netstandard2.0\AutoMapper.dll</Reference> <NuGetReference>BenchmarkDotNet</NuGetReference> <Namespace>AutoMapper</Namespace> <Namespace>BenchmarkDotNet.Attributes</Namespace> <Namespace>BenchmarkDotNet.Running</Namespace> <Namespace>System.Security.Cryptography</Namespace> <Namespace>System.Windows.Forms.DataVisualization.Charting</Namespace> </Query> #LINQPad optimize+ /* 对象克隆(C# 快速高效率复制对象另一种方式 表达式树转) https://www.cnblogs.com/lsgsanxiao/p/8205096.html */ void Main() { Student s = InitStudent(); //StudentSecond ss= TransReflection<Student, StudentSecond>(s); StudentSecond ss= TransExpV2<Student, StudentSecond>.Trans(s); ss.Dump(); var summary = BenchmarkRunner.Run<CloneBenchmarks>(); } public static Student InitStudent(){ List<Address> studentAddress = new List<Address>(); studentAddress.Add( new Address(){address= "testaddress1" }); studentAddress.Add( new Address(){address= "testaddress2" }); Student s = new Student() { Age = 20, Id = 1, Name = "Emrys" , StudentAddress = studentAddress}; return s; } public class Student { public int Id { get ; set ; } public string Name { get ; set ; } public int Age { get ; set ; } public List<Address> StudentAddress { get ; set ; } } public class StudentSecond { public int Id { get ; set ; } public string Name { get ; set ; } public int Age { get ; set ; } public List<Address> StudentAddress { get ; set ; } } public class Address { public string address { get ; set ; } } public class CloneBenchmarks { AutoMapper.IMapper _mapper; Student _s; public CloneBenchmarks(){ var config = new MapperConfiguration(cfg => { cfg.CreateMap<Student, StudentSecond>(); cfg.CreateMap<Address, Address>(); }); config.AssertConfigurationIsValid(); _mapper = config.CreateMapper(); _s = InitStudent(); } [Benchmark] public object TestTransReflection(){ StudentSecond ss= TransReflection<Student, StudentSecond>(_s); return ss; } [Benchmark] public object TestExpression(){ Expression<Func<Student, StudentSecond>> ss = (x) => new StudentSecond { Age = x.Age, Id = x.Id, Name = x.Name , StudentAddress = x.StudentAddress }; var f = ss.Compile(); StudentSecond studentSecond = f(_s); return studentSecond; } private static Dictionary< string , object > _Dic = new Dictionary< string , object >(); [Benchmark] public object TestTransExp(){ StudentSecond ss= TransExp<Student, StudentSecond>(_s); return ss; } [Benchmark] public object TestTransExpV2(){ StudentSecond ss= TransExpV2<Student, StudentSecond>.Trans(_s); return ss; } [Benchmark] public object TestAutoMappe(){ var ss = _mapper.Map<Student, StudentSecond>(_s); return ss; } public TOut TransReflection<TIn, TOut>(TIn tIn) { TOut tOut = Activator.CreateInstance<TOut>(); var tInType = tIn.GetType(); foreach ( var itemOut in tOut.GetType().GetProperties()) { var itemIn = tInType.GetProperty(itemOut.Name); ; if (itemIn != null ) { itemOut.SetValue(tOut, itemIn.GetValue(tIn)); } } return tOut; } private static TOut TransExp<TIn, TOut>(TIn tIn) { string key = string .Format( "trans_exp_{0}_{1}" , typeof (TIn).FullName, typeof (TOut).FullName); if (!_Dic.ContainsKey(key)) { ParameterExpression parameterExpression = Expression.Parameter( typeof (TIn), "p" ); List<MemberBinding> memberBindingList = new List<MemberBinding>(); foreach ( var item in typeof (TOut).GetProperties()) { if (!item.CanWrite) continue ; MemberExpression property = Expression.Property(parameterExpression, typeof (TIn).GetProperty(item.Name)); MemberBinding memberBinding = Expression.Bind(item, property); memberBindingList.Add(memberBinding); } MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New( typeof (TOut)), memberBindingList.ToArray()); Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression }); Func<TIn, TOut> func = lambda.Compile(); _Dic[key] = func; } return ((Func<TIn, TOut>)_Dic[key])(tIn); } } public static class TransExpV2<TIn, TOut> { private static readonly Func<TIn, TOut> cache = GetFunc(); private static Func<TIn, TOut> GetFunc() { ParameterExpression parameterExpression = Expression.Parameter( typeof (TIn), "p" ); List<MemberBinding> memberBindingList = new List<MemberBinding>(); foreach ( var item in typeof (TOut).GetProperties()) { if (!item.CanWrite) continue ; MemberExpression property = Expression.Property(parameterExpression, typeof (TIn).GetProperty(item.Name)); MemberBinding memberBinding = Expression.Bind(item, property); memberBindingList.Add(memberBinding); } MemberInitExpression memberInitExpression = Expression.MemberInit(Expression.New( typeof (TOut)), memberBindingList.ToArray()); Expression<Func<TIn, TOut>> lambda = Expression.Lambda<Func<TIn, TOut>>(memberInitExpression, new ParameterExpression[] { parameterExpression }); return lambda.Compile(); } public static TOut Trans(TIn tIn) { return cache(tIn); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!