Skip to content

Clone an IDeepCloneable collection by its own DeepClone in the IEnumerable extension [patch] - #141

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/140-enumerable-deepclone-honors-ideepcloneable
Nov 10, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/140-enumerable-deepclone-honors-ideepcloneable

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #140

What changed

Some types implement IDeepCloneable.DeepClone() explicitly, as IDeepCloneable's remarks recommend, and are also IEnumerable<T>. Such a type has no accessible instance DeepClone(), so bag.DeepClone() bound to DeepClone<T>(this IEnumerable<T>), which returned a compiler-generated read-only list of cloned items. The clone lost the type, its own clone logic and every field that wasn't an element.

  • DeepClone<T>(this IEnumerable<T> source) now checks source is IDeepCloneable first and returns (IEnumerable<T>)cloneable.DeepClone(). Otherwise it clones element by element as before.
  • The XML docs on that overload and the remarks on IDeepCloneable.DeepClone() describe the binding.

Tests

CloneableCollectionTests.Enumerable_DeepClone_OfIDeepCloneableCollection_ShouldUseItsOwnClone is the repro from the issue. The clone is a Bag (not <>z__ReadOnlyList1), keeps its Label, and holds a distinct cloned item. The test fails on mainwith "Expected value to be of type Bag" and passes with the fix. The full suite passes (104/104), anddotnet build` across all target frameworks has 0 warnings.

Note

Open PR #132 also edits DeepCloneContainerExtensions.cs near this overload, and the two conflict textually. Whichever merges second needs a small rebase.


Generated by Claude Code

…rable extension [patch]

A type that implements IDeepCloneable.DeepClone() explicitly and is also an
IEnumerable<T> has no instance DeepClone(), so collection.DeepClone() bound to
the IEnumerable<T> extension and returned a read-only list of cloned items,
losing the type and its other fields. The extension now calls the source's
own IDeepCloneable.DeepClone() when it has one, and IDeepCloneable's remarks
describe the binding.

Fixes #140

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW2o6qTyeFzioJm18xz
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 09572b6 into main Nov 10, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/140-enumerable-deepclone-honors-ideepcloneable branch November 10, 2026 23:46
matt-edmondson pushed a commit that referenced this pull request Nov 11, 2026
Resolve the conflicts with #129, #131, #133, #141 and #142, and route the
overloads they added through the clone context:
- DeepCloneFrom keeps EnsureSameComparer before cloning with CloneAll/CloneAllPairs
- the IEnumerable overload keeps the IDeepCloneable short-circuit, then CloneAll
- T[], ConcurrentQueue, ConcurrentBag, FrozenDictionary and FrozenSet clone through
  CloneAll/CloneAllPairs, so one object held twice comes back as one clone twice
- ClonePairs, which no caller uses any more, is gone

New CloneContextTests cover the array, ConcurrentQueue and ConcurrentBag cases;
they fail with those overloads cloning element by element outside the context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW5qqNjheJmiNmh23zr
matt-edmondson added a commit that referenced this pull request Nov 13, 2026
… implementation in every overload [patch] (#150)

* Clone a collection subclass that implements IDeepCloneable by its own implementation in every overload [patch]

#141 made only the IEnumerable<T> overload defer to a source's own
IDeepCloneable.DeepClone(). A List<T>, HashSet<T>, Dictionary<,>,
Queue<T> or other concrete-type subclass bound to its more specific
overload and was cloned element by element, losing its type and state.

A shared CloneItself helper now runs first in every collection overload
whose type can be derived from. It opens the clone context, so an item
the collection holds twice is cloned once, and it falls through to the
element-wise clone when the implementation calls the base overload on
itself, instead of recursing.

Fixes #148

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3faRnes8WmPvx9zps

* Test that every derivable collection overload defers to the subclass's own clone

SonarCloud reported 71% coverage on new code: the early return in the
IDictionary, IReadOnlyDictionary, SortedDictionary, SortedList,
OrderedDictionary, ConcurrentDictionary, ReadOnlyDictionary, LinkedList,
SortedSet, Stack and Concurrent* overloads had no test. One test now
sends a marked subclass through each of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017vfJW3faRnes8WmPvx9zps

---------

Co-authored-by: matt-edmondson <19528727+matt-edmondson@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bag.DeepClone() on a collection type that implements IDeepCloneable explicitly returns a read-only list, not a Bag: the type's own clone never runs

2 participants