Repository navigation
Clone an IDeepCloneable collection by its own DeepClone in the IEnumerable extension [patch] - #141
Merged
matt-edmondson merged 1 commit intoNov 10, 2026
Conversation
…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
|
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
This was referenced Nov 12, 2026
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>
This was referenced Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #140
What changed
Some types implement
IDeepCloneable.DeepClone()explicitly, asIDeepCloneable's remarks recommend, and are alsoIEnumerable<T>. Such a type has no accessible instanceDeepClone(), sobag.DeepClone()bound toDeepClone<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 checkssource is IDeepCloneablefirst and returns(IEnumerable<T>)cloneable.DeepClone(). Otherwise it clones element by element as before.IDeepCloneable.DeepClone()describe the binding.Tests
CloneableCollectionTests.Enumerable_DeepClone_OfIDeepCloneableCollection_ShouldUseItsOwnCloneis the repro from the issue. The clone is aBag(not<>z__ReadOnlyList1), keeps itsLabel, and holds a distinct cloned item. The test fails onmainwith "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.csnear this overload, and the two conflict textually. Whichever merges second needs a small rebase.Generated by Claude Code