@@ -212,47 +212,72 @@ private static void WriteGetterOnlyCollectionPopulation(CloneGeneratorContext co
212212 if ( member . TypeKind != MemberTypeKind . Collection && member . TypeKind != MemberTypeKind . Dictionary )
213213 return ;
214214
215- // Get the helper method that clones this collection type
216- string helperMethodName = context . GetOrCreateHelperMethodName ( member ) ;
217- bool memberNeedsState = MemberNeedsCircularRefTracking ( context , member ) ;
218- string actualStateVar = memberNeedsState ? stateVar : "null" ;
219-
220- // Generate unique variable name for the cloned collection
221- string clonedVar = $ "cloned_{ context . GetNextVariableId ( ) } ";
222-
223215 // Check if source collection is null
224216 sb . AppendLine ( $ " if ({ sourceVar } .{ memberName } != null)") ;
225217 sb . AppendLine ( " {" ) ;
226218
227- // Clone the source collection using the existing helper (handles all the complexity)
228- string helperCall = GetHelperMethodCall ( context , helperMethodName , $ "{ sourceVar } .{ memberName } ", memberNeedsState , actualStateVar ) ;
229- sb . AppendLine ( $ " var { clonedVar } = { helperCall } ;") ;
230- sb . AppendLine ( $ " if ({ clonedVar } != null)") ;
231- sb . AppendLine ( " {" ) ;
232-
233- // Clear target and copy from cloned collection
234- sb . AppendLine ( $ " { resultVar } .{ memberName } .Clear();") ;
219+ // Clear target collection first
220+ sb . AppendLine ( $ " { resultVar } .{ memberName } .Clear();") ;
235221
236- if ( member . TypeKind == MemberTypeKind . Dictionary )
222+ // When [FastClonerShallow] is applied to getter-only collection, shallow clone items (just copy references)
223+ if ( member . IsShallowClone )
237224 {
238- // For dictionaries, copy key-value pairs
239- sb . AppendLine ( $ " foreach (var kvp in { clonedVar } )") ;
240- sb . AppendLine ( " {" ) ;
241- // Use indexer - works for all dictionary types including ConcurrentDictionary
242- sb . AppendLine ( $ " { resultVar } .{ memberName } [kvp.Key] = kvp.Value;") ;
243- sb . AppendLine ( " }" ) ;
225+ if ( member . TypeKind == MemberTypeKind . Dictionary )
226+ {
227+ // For dictionaries, copy key-value pairs directly (shallow)
228+ sb . AppendLine ( $ " foreach (var kvp in { sourceVar } .{ memberName } )") ;
229+ sb . AppendLine ( " {" ) ;
230+ sb . AppendLine ( $ " { resultVar } .{ memberName } [kvp.Key] = kvp.Value;") ;
231+ sb . AppendLine ( " }" ) ;
232+ }
233+ else
234+ {
235+ // For collections, add items directly (shallow)
236+ string addMethod = GetAddMethodForCollection ( member . CollectionKind ) ;
237+ sb . AppendLine ( $ " foreach (var item in { sourceVar } .{ memberName } )") ;
238+ sb . AppendLine ( " {" ) ;
239+ sb . AppendLine ( $ " { resultVar } .{ memberName } .{ addMethod } (item);") ;
240+ sb . AppendLine ( " }" ) ;
241+ }
244242 }
245243 else
246244 {
247- // For collections, use the appropriate add method based on collection kind
248- string addMethod = GetAddMethodForCollection ( member . CollectionKind ) ;
249- sb . AppendLine ( $ " foreach (var item in { clonedVar } )") ;
250- sb . AppendLine ( " {" ) ;
251- sb . AppendLine ( $ " { resultVar } .{ memberName } .{ addMethod } (item);") ;
252- sb . AppendLine ( " }" ) ;
245+ // Deep clone: Get the helper method that clones this collection type
246+ string helperMethodName = context . GetOrCreateHelperMethodName ( member ) ;
247+ bool memberNeedsState = MemberNeedsCircularRefTracking ( context , member ) ;
248+ string actualStateVar = memberNeedsState ? stateVar : "null" ;
249+
250+ // Generate unique variable name for the cloned collection
251+ string clonedVar = $ "cloned_{ context . GetNextVariableId ( ) } ";
252+
253+ // Clone the source collection using the existing helper (handles all the complexity)
254+ string helperCall = GetHelperMethodCall ( context , helperMethodName , $ "{ sourceVar } .{ memberName } ", memberNeedsState , actualStateVar ) ;
255+ sb . AppendLine ( $ " var { clonedVar } = { helperCall } ;") ;
256+ sb . AppendLine ( $ " if ({ clonedVar } != null)") ;
257+ sb . AppendLine ( " {" ) ;
258+
259+ if ( member . TypeKind == MemberTypeKind . Dictionary )
260+ {
261+ // For dictionaries, copy key-value pairs
262+ sb . AppendLine ( $ " foreach (var kvp in { clonedVar } )") ;
263+ sb . AppendLine ( " {" ) ;
264+ // Use indexer - works for all dictionary types including ConcurrentDictionary
265+ sb . AppendLine ( $ " { resultVar } .{ memberName } [kvp.Key] = kvp.Value;") ;
266+ sb . AppendLine ( " }" ) ;
267+ }
268+ else
269+ {
270+ // For collections, use the appropriate add method based on collection kind
271+ string addMethod = GetAddMethodForCollection ( member . CollectionKind ) ;
272+ sb . AppendLine ( $ " foreach (var item in { clonedVar } )") ;
273+ sb . AppendLine ( " {" ) ;
274+ sb . AppendLine ( $ " { resultVar } .{ memberName } .{ addMethod } (item);") ;
275+ sb . AppendLine ( " }" ) ;
276+ }
277+
278+ sb . AppendLine ( " }" ) ;
253279 }
254280
255- sb . AppendLine ( " }" ) ;
256281 sb . AppendLine ( " }" ) ;
257282 }
258283
0 commit comments