11using System . Linq . Expressions ;
22using System . Reflection ;
33
4- namespace FastCloner . Helpers ;
4+ namespace FastCloner . Code ;
55
66internal static class ClonerToExprGenerator
77{
@@ -27,7 +27,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
2727 ParameterExpression ? fromLocal = from ;
2828 ParameterExpression ? to = Expression . Parameter ( methodType ) ;
2929 ParameterExpression ? toLocal = to ;
30- ParameterExpression ? state = Expression . Parameter ( typeof ( DeepCloneState ) ) ;
30+ ParameterExpression ? state = Expression . Parameter ( typeof ( FastCloneState ) ) ;
3131
3232 // if (!type.IsValueType())
3333 {
@@ -42,7 +42,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
4242 // added from -> to binding to ensure reference loop handling
4343 // structs cannot loop here
4444 // state.AddKnownRef(from, to)
45- expressionList . Add ( Expression . Call ( state , typeof ( DeepCloneState ) . GetMethod ( nameof ( DeepCloneState . AddKnownRef ) ) ! , from , to ) ) ;
45+ expressionList . Add ( Expression . Call ( state , typeof ( FastCloneState ) . GetMethod ( nameof ( FastCloneState . AddKnownRef ) ) ! , from , to ) ) ;
4646 }
4747 }
4848
@@ -59,7 +59,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
5959
6060 foreach ( FieldInfo ? fieldInfo in fi )
6161 {
62- if ( isDeepClone && ! DeepClonerSafeTypes . CanReturnSameObject ( fieldInfo . FieldType ) )
62+ if ( isDeepClone && ! FastClonerSafeTypes . CanReturnSameObject ( fieldInfo . FieldType ) )
6363 {
6464 MethodInfo ? methodInfo = fieldInfo . FieldType . IsValueType ( )
6565 ? StaticMethodInfos . DeepClonerGeneratorMethods . CloneStructInternal . MakeGenericMethod ( fieldInfo . FieldType )
@@ -78,7 +78,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
7878 {
7979 // var setMethod = fieldInfo.GetType().GetMethod("SetValue", new[] { typeof(object), typeof(object) });
8080 // expressionList.Add(Expression.Call(Expression.Constant(fieldInfo), setMethod, toLocal, call));
81- MethodInfo ? setMethod = typeof ( DeepClonerExprGenerator ) . GetPrivateStaticMethod ( nameof ( DeepClonerExprGenerator . ForceSetField ) ) ! ;
81+ MethodInfo ? setMethod = typeof ( FastClonerExprGenerator ) . GetPrivateStaticMethod ( nameof ( FastClonerExprGenerator . ForceSetField ) ) ! ;
8282 expressionList . Add ( Expression . Call ( setMethod , Expression . Constant ( fieldInfo ) ,
8383 Expression . Convert ( toLocal , typeof ( object ) ) , Expression . Convert ( call , typeof ( object ) ) ) ) ;
8484 }
@@ -95,7 +95,7 @@ private static object GenerateProcessMethod(Type type, bool isDeepClone)
9595
9696 expressionList . Add ( Expression . Convert ( toLocal , methodType ) ) ;
9797
98- Type ? funcType = typeof ( Func < , , , > ) . MakeGenericType ( methodType , methodType , typeof ( DeepCloneState ) , methodType ) ;
98+ Type ? funcType = typeof ( Func < , , , > ) . MakeGenericType ( methodType , methodType , typeof ( FastCloneState ) , methodType ) ;
9999
100100 List < ParameterExpression > ? blockParams = [ ] ;
101101 if ( from != fromLocal ) blockParams . Add ( fromLocal ) ;
@@ -111,9 +111,9 @@ private static object GenerateProcessArrayMethod(Type type, bool isDeep)
111111
112112 ParameterExpression from = Expression . Parameter ( typeof ( object ) ) ;
113113 ParameterExpression to = Expression . Parameter ( typeof ( object ) ) ;
114- ParameterExpression ? state = Expression . Parameter ( typeof ( DeepCloneState ) ) ;
114+ ParameterExpression ? state = Expression . Parameter ( typeof ( FastCloneState ) ) ;
115115
116- Type ? funcType = typeof ( Func < , , , > ) . MakeGenericType ( typeof ( object ) , typeof ( object ) , typeof ( DeepCloneState ) , typeof ( object ) ) ;
116+ Type ? funcType = typeof ( Func < , , , > ) . MakeGenericType ( typeof ( object ) , typeof ( object ) , typeof ( FastCloneState ) , typeof ( object ) ) ;
117117
118118 if ( rank == 1 && type == elementType . MakeArrayType ( ) )
119119 {
@@ -127,7 +127,7 @@ private static object GenerateProcessArrayMethod(Type type, bool isDeep)
127127 else
128128 {
129129 string ? methodName = nameof ( Clone1DimArrayClassInternal ) ;
130- if ( DeepClonerSafeTypes . CanReturnSameObject ( elementType ) ) methodName = nameof ( Clone1DimArraySafeInternal ) ;
130+ if ( FastClonerSafeTypes . CanReturnSameObject ( elementType ) ) methodName = nameof ( Clone1DimArraySafeInternal ) ;
131131 else if ( elementType . IsValueType ( ) ) methodName = nameof ( Clone1DimArrayStructInternal ) ;
132132 MethodInfo ? methodInfo = typeof ( ClonerToExprGenerator ) . GetPrivateStaticMethod ( methodName ) ! . MakeGenericMethod ( elementType ) ;
133133 MethodCallExpression ? callS = Expression . Call ( methodInfo , Expression . Convert ( from , type ) , Expression . Convert ( to , type ) , state ) ;
@@ -157,40 +157,40 @@ internal static T[] ShallowClone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo)
157157 }
158158
159159 // when we can't use code generation, we can use these methods
160- internal static T [ ] Clone1DimArraySafeInternal < T > ( T [ ] objFrom , T [ ] objTo , DeepCloneState state )
160+ internal static T [ ] Clone1DimArraySafeInternal < T > ( T [ ] objFrom , T [ ] objTo , FastCloneState state )
161161 {
162162 int l = Math . Min ( objFrom . Length , objTo . Length ) ;
163163 state . AddKnownRef ( objFrom , objTo ) ;
164164 Array . Copy ( objFrom , objTo , l ) ;
165165 return objTo ;
166166 }
167167
168- internal static T [ ] ? Clone1DimArrayStructInternal < T > ( T [ ] ? objFrom , T [ ] ? objTo , DeepCloneState state )
168+ internal static T [ ] ? Clone1DimArrayStructInternal < T > ( T [ ] ? objFrom , T [ ] ? objTo , FastCloneState state )
169169 {
170170 // not null from called method, but will check it anyway
171171 if ( objFrom == null || objTo == null ) return null ;
172172 int l = Math . Min ( objFrom . Length , objTo . Length ) ;
173173 state . AddKnownRef ( objFrom , objTo ) ;
174- Func < T , DeepCloneState , T > ? cloner = DeepClonerGenerator . GetClonerForValueType < T > ( ) ;
174+ Func < T , FastCloneState , T > ? cloner = FastClonerGenerator . GetClonerForValueType < T > ( ) ;
175175 for ( int i = 0 ; i < l ; i ++ )
176176 objTo [ i ] = cloner ( objTo [ i ] , state ) ;
177177
178178 return objTo ;
179179 }
180180
181- internal static T [ ] ? Clone1DimArrayClassInternal < T > ( T [ ] ? objFrom , T [ ] ? objTo , DeepCloneState state )
181+ internal static T [ ] ? Clone1DimArrayClassInternal < T > ( T [ ] ? objFrom , T [ ] ? objTo , FastCloneState state )
182182 {
183183 // not null from called method, but will check it anyway
184184 if ( objFrom == null || objTo == null ) return null ;
185185 int l = Math . Min ( objFrom . Length , objTo . Length ) ;
186186 state . AddKnownRef ( objFrom , objTo ) ;
187187 for ( int i = 0 ; i < l ; i ++ )
188- objTo [ i ] = ( T ) DeepClonerGenerator . CloneClassInternal ( objFrom [ i ] , state ) ! ;
188+ objTo [ i ] = ( T ) FastClonerGenerator . CloneClassInternal ( objFrom [ i ] , state ) ! ;
189189
190190 return objTo ;
191191 }
192192
193- internal static T [ , ] ? Clone2DimArrayInternal < T > ( T [ , ] ? objFrom , T [ , ] ? objTo , DeepCloneState state , bool isDeep )
193+ internal static T [ , ] ? Clone2DimArrayInternal < T > ( T [ , ] ? objFrom , T [ , ] ? objTo , FastCloneState state , bool isDeep )
194194 {
195195 // not null from called method, but will check it anyway
196196 if ( objFrom == null || objTo == null ) return null ;
@@ -201,7 +201,7 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, DeepCl
201201 int l1 = Math . Min ( objFrom . GetLength ( 0 ) , objTo . GetLength ( 0 ) ) ;
202202 int l2 = Math . Min ( objFrom . GetLength ( 1 ) , objTo . GetLength ( 1 ) ) ;
203203 state . AddKnownRef ( objFrom , objTo ) ;
204- if ( ( ! isDeep || DeepClonerSafeTypes . CanReturnSameObject ( typeof ( T ) ) )
204+ if ( ( ! isDeep || FastClonerSafeTypes . CanReturnSameObject ( typeof ( T ) ) )
205205 && objFrom . GetLength ( 0 ) == objTo . GetLength ( 0 )
206206 && objFrom . GetLength ( 1 ) == objTo . GetLength ( 1 ) )
207207 {
@@ -219,7 +219,7 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, DeepCl
219219
220220 if ( typeof ( T ) . IsValueType ( ) )
221221 {
222- Func < T , DeepCloneState , T > ? cloner = DeepClonerGenerator . GetClonerForValueType < T > ( ) ;
222+ Func < T , FastCloneState , T > ? cloner = FastClonerGenerator . GetClonerForValueType < T > ( ) ;
223223 for ( int i = 0 ; i < l1 ; i ++ )
224224 for ( int k = 0 ; k < l2 ; k ++ )
225225 objTo [ i , k ] = cloner ( objFrom [ i , k ] , state ) ;
@@ -228,14 +228,14 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, DeepCl
228228 {
229229 for ( int i = 0 ; i < l1 ; i ++ )
230230 for ( int k = 0 ; k < l2 ; k ++ )
231- objTo [ i , k ] = ( T ) DeepClonerGenerator . CloneClassInternal ( objFrom [ i , k ] , state ) ! ;
231+ objTo [ i , k ] = ( T ) FastClonerGenerator . CloneClassInternal ( objFrom [ i , k ] , state ) ! ;
232232 }
233233
234234 return objTo ;
235235 }
236236
237237 // rare cases, very slow cloning. currently it's ok
238- internal static Array ? CloneAbstractArrayInternal ( Array ? objFrom , Array ? objTo , DeepCloneState state , bool isDeep )
238+ internal static Array ? CloneAbstractArrayInternal ( Array ? objFrom , Array ? objTo , FastCloneState state , bool isDeep )
239239 {
240240 // not null from called method, but will check it anyway
241241 if ( objFrom == null || objTo == null ) return null ;
@@ -259,7 +259,7 @@ internal static T[] Clone1DimArraySafeInternal<T>(T[] objFrom, T[] objTo, DeepCl
259259 {
260260 objTo . SetValue (
261261 isDeep
262- ? DeepClonerGenerator . CloneClassInternal (
262+ ? FastClonerGenerator . CloneClassInternal (
263263 objFrom . GetValue ( idxesFrom ) ,
264264 state )
265265 : objFrom . GetValue ( idxesFrom ) , idxesTo ) ;
0 commit comments