Skip to content

[patch] Keep the wrapped dictionary's comparer when cloning a ReadOnlyDictionary - #99

Merged
matt-edmondson merged 2 commits into
mainfrom
fix/readonly-dictionary-comparer-87
Oct 6, 2026
Merged

matt-edmondson merged 2 commits into
mainfrom
fix/readonly-dictionary-comparer-87

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Summary

CloneDictionary picks the clone's kind and comparer from the runtime type of the object it is given. For a ReadOnlyDictionary<,> that object is the wrapper, which matches none of the known arms, so the clone always fell back to a default Dictionary<,>. Two things went wrong as a result:

  • A read-only view over an OrdinalIgnoreCase dictionary cloned to one where ContainsKey("KEY") was false.
  • A wrapped dictionary with a stricter comparer, such as ReferenceEqualityComparer holding equal but distinct keys, made the clone throw ArgumentException.

CloneDictionary now looks through any ReadOnlyDictionary wrappers, nested ones included, to the dictionary they wrap before it picks the clone's kind and comparer. ReadOnlyDictionary exposes that dictionary only through its protected Dictionary property. 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 concrete ReadOnlyDictionary overload, IReadOnlyDictionary, and IDictionary.

Tests

The new tests in DictionaryComparerTests are:

  • ReadOnlyDictionary over an OrdinalIgnoreCase dictionary: ContainsKey("KEY") is true on the clone through the concrete overload, through IReadOnlyDictionary and through IDictionary.
  • ReadOnlyDictionary over a reference-equality dictionary with two equal but distinct keys: the clone keeps both keys.
  • ReadOnlyDictionary(ReadOnlyDictionary(SortedDictionary(descending))): the clone is a SortedDictionary and 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

claude added 2 commits October 6, 2026 00:30
…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
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

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.

ReadOnlyDictionary.DeepClone() drops the wrapped dictionary's key comparer: a case-insensitive source's clone can't find "KEY"

2 participants