Repository navigation
[patch] Keep the wrapped dictionary's comparer when cloning a ReadOnlyDictionary - #99
Merged
Merged
Conversation
…yDictionary CloneDictionary picked the clone's kind and comparer from the runtime type of the object it was given. For a ReadOnlyDictionary that is the wrapper, which matched none of the known types, so the clone always fell back to a default Dictionary. A read-only view over a case-insensitive dictionary therefore cloned to one where "KEY" no longer matched "Key". A stricter comparer also made the clone throw on duplicate keys. Look through ReadOnlyDictionary wrappers to the dictionary they wrap, read once per closed type through a cached delegate over the protected Dictionary property, before choosing the clone's kind and comparer. Fixes #87 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb
…nary SonarCloud flags the protected-property getter under S3011. It only reads the wrapped dictionary, and the issue's triage chose that approach over losing the comparer, so suppress the rule on that field with the reason. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb
|
This was referenced Oct 7, 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.



Summary
CloneDictionarypicks the clone's kind and comparer from the runtime type of the object it is given. For aReadOnlyDictionary<,>that object is the wrapper, which matches none of the known arms, so the clone always fell back to a defaultDictionary<,>. Two things went wrong as a result:OrdinalIgnoreCasedictionary cloned to one whereContainsKey("KEY")was false.ReferenceEqualityComparerholding equal but distinct keys, made the clone throwArgumentException.CloneDictionarynow looks through anyReadOnlyDictionarywrappers, nested ones included, to the dictionary they wrap before it picks the clone's kind and comparer.ReadOnlyDictionaryexposes that dictionary only through its protectedDictionaryproperty. Per the triage note, it is read through a cached, strongly typed delegate built once per closed generic type, not by reflection on every call. This fixes all three entry points: the concreteReadOnlyDictionaryoverload,IReadOnlyDictionary, andIDictionary.Tests
The new tests in
DictionaryComparerTestsare:ReadOnlyDictionaryover anOrdinalIgnoreCasedictionary:ContainsKey("KEY")is true on the clone through the concrete overload, throughIReadOnlyDictionaryand throughIDictionary.ReadOnlyDictionaryover a reference-equality dictionary with two equal but distinct keys: the clone keeps both keys.ReadOnlyDictionary(ReadOnlyDictionary(SortedDictionary(descending))): the clone is aSortedDictionaryand keeps the descending order after an insert.I ran all five tests against the unfixed code first and all five failed. With the fix the full suite passes, 63/63 on net10.0. The library builds for net10.0, net9.0, net8.0, netstandard2.0 and netstandard2.1.
This PR is independent of #98 (#86, direct-call overloads), and the two merge cleanly in either order.
Fixes #87
🤖 Generated with Claude Code
https://claude.ai/code/session_01UoSUYMQMykgfsb5y6AhnKb
Generated by Claude Code