Deep clone the key and value of a KeyValuePair element [patch] - #91
Merged
Merged
Conversation
A dictionary reached only as a sequence of pairs, such as an IReadOnlyDictionary passed to DeepCloneFrom or LINQ over a dictionary, went through the element-wise overloads with T = KeyValuePair. The pair never implements IDeepCloneable, so it was copied as-is and the clone shared its keys and values with the source. The element helper now rebuilds a KeyValuePair from deep clones of its key and value. Fixes #82 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NFZqEcFcZzrKRzQvofupG6
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NFZqEcFcZzrKRzQvofupG6
… reflection [patch] Binding a private method with BindingFlags.NonPublic tripped Sonar's accessibility-bypass rule (S3011). A private generic cloner class created with Activator needs no non-public lookup, and it also moves the pair-cloning logic into its own type (S3398). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NFZqEcFcZzrKRzQvofupG6
|
This was referenced Sep 30, 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 #82
What was wrong
Some sources are sequences of
KeyValuePair<TKey, TValue>but aren't staticallyIDictionary<TKey, TValue>, such as anIReadOnlyDictionarypassed toDeepCloneFromor LINQ over a dictionary. For these, overload resolution picks the element-wise overloads withT = KeyValuePair<,>. The private element helper only clonedIDeepCloneableinstances, so the pair was copied as-is. The "clone" then shared its keys and values with the source.Change
The private
DeepClone<T>element helper now rebuilds aKeyValuePair<,>from deep clones of its key and value. This is the lower-risk option from the triage: no new public overloads, so it can't reintroduce the CS0121 ambiguity from #81.A generic static cache,
PairCloner<T>, checks once per element type whetherTis aKeyValuePair<,>. If it is, the cache creates a privatePairElementCloner<TKey, TValue>withActivator, so no non-public reflection is needed. For every other element type the extra cost is a null check.Tests
In
EnumerableCloneTests:DeepCloneFrom_ReadOnlyDictionarySource_ShouldCloneKeysAndValues: the issue's first repro. Key and value are no longer the source instances.Enumerable_DeepClone_LinqOverDictionary_ShouldCloneKeysAndValues: the issue's second repro,src.Where(...).DeepClone().Enumerable_DeepClone_PairsOfNonCloneables_ShouldKeepTheirContents: pairs of non-cloneables still pass through unchanged.The first two fail with the fix reverted. The full suite passes (51/51), and the library builds clean for every target framework.
🤖 Generated with Claude Code
https://claude.ai/code/session_01NFZqEcFcZzrKRzQvofupG6