You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
netstandard2.0/2.1: immutableDictionary.DeepClone() fails to compile (CS0121), and the cast workaround returns a plain Dictionary without the key comparer #116
#86 and #98 fixed the CS0121 ambiguity and the lost key comparer for ImmutableDictionary and ImmutableSortedDictionary, but only for the .NET builds. All of the immutable handling is behind #if NET:
the typed overloads in DeepClone/DeepCloneContainerExtensions.cs L284-L338
the immutable cases in CloneDictionary at L450-L454
The package also ships netstandard2.0 and netstandard2.1 builds (ktsu.Sdk TFMs: net10.0, net9.0, net8.0, netstandard2.0, netstandard2.1). DeepClone.csproj references System.Memory and System.Threading.Tasks.Extensions for those targets, but not System.Collections.Immutable. Consumers on .NET Framework, Unity or Mono still hit both of the original problems:
Compile error.immutableDict.DeepClone() and immutableSortedDict.DeepClone() fail with CS0121, because both types implement both IDictionary<,> and IReadOnlyDictionary<,>.
Silent data change. Casting to either interface to get past the error falls through to _ => new Dictionary<TKey, TValue>() (L455). The clone loses the key comparer, and an ImmutableSortedDictionary comes back as an unsorted, mutable Dictionary.
The README table (L612-L613) marks those overloads "(.NET only)". It doesn't mention that the plain call fails to compile on netstandard, or that the cast workaround drops the comparer.
Repro
1. In a netstandard2.0 class library with a ProjectReference to DeepClone.csproj and System.Collections.Immutable 8.0.0:
error CS0121: The call is ambiguous between 'DeepClone<TKey, TValue>(IDictionary<TKey, TValue>)' and 'DeepClone<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)'
2. Calling the netstandard2.0 ktsu.DeepClone.dll through the interface casts:
ImmutableDictionary via IDictionary -> Dictionary`2, ContainsKey("KEY")=False (source: True)
via IReadOnlyDictionary -> ContainsKey("KEY")=False
Suggested fix / acceptance criteria
Add a conditional PackageReference to System.Collections.Immutable for the netstandard TFMs, the same way System.Memory is referenced.
Widen the #if NET guards around the immutable overloads and the CloneDictionary cases so the netstandard builds get them too.
If the extra dependency isn't wanted, document the CS0121 and the comparer loss next to the "(.NET only)" rows instead.
Done when:
a netstandard2.0 consumer compiles immutableDict.DeepClone() and immutableSortedDict.DeepClone();
the clones keep their KeyComparer;
the comparer tests pass against the netstandard2.0 build.
Area: netstandard2.0/2.1 builds: the #if NET guards in DeepClone/DeepCloneContainerExtensions.cs (L284-L338, L450-L454) and the netstandard package references in DeepClone.csproj
Suggested assignment: DeepClone maintainer. No team mapping is set for this area.
Assessment: There are two problems on a shipped target:
A plain DeepClone() call on an immutable dictionary fails to compile (CS0121).
The obvious cast workaround compiles but silently returns a mutable, unsorted Dictionary without the key comparer.
The second is a silent data change: lookups that should succeed case-insensitively return false. I've rated it High rather than Medium because of that. .NET Framework, Unity and Mono consumers get no warning. The preferred fix is to add a conditional System.Collections.Immutable PackageReference and widen the guards. If the extra dependency isn't wanted, the fallback is to document both problems.
Activity
matt-edmondson commentedon Nov 1, 2026
Triage
#if NETguards inDeepClone/DeepCloneContainerExtensions.cs(L284-L338, L450-L454) and the netstandard package references inDeepClone.csprojAssessment: There are two problems on a shipped target:
DeepClone()call on an immutable dictionary fails to compile (CS0121).Dictionarywithout the key comparer.The second is a silent data change: lookups that should succeed case-insensitively return false. I've rated it High rather than Medium because of that. .NET Framework, Unity and Mono consumers get no warning. The preferred fix is to add a conditional System.Collections.Immutable
PackageReferenceand widen the guards. If the extra dependency isn't wanted, the fallback is to document both problems.Related / duplicates:
Immutable*overloads missing or wrong). The same netstandard System.Collections.Immutable dependency would unblock both.Generated by Claude Code