feat(serialization): add codecs for FrozenDictionary and FrozenSet

Merged
RE
3mo ago
+454-13

Problem

Orleans had no serialization support for the frozen collection types introduced in .NET 8 (FrozenDictionary<TKey, TValue> and FrozenSet<T>). Attempting to serialize a grain call, storage payload, or stream event containing one of these types would fail.

Solution

Add serialization codecs and copiers for FrozenDictionary<TKey, TValue> and FrozenSet<T>, modeled on the existing immutable-collection codecs. They are guarded by #if NET8_0_OR_GREATER so they only compile on platforms that ship the frozen collections (the netstandard2.1 target excludes them).

Notable details:

  • Surrogates carry the entries as a List<> plus the collection's comparer (stored only when non-default). Reading entries straight out of the source collection avoids re-hashing keys when constructing the surrogate.
  • Copiers deep-copy their contents in the same manner as the immutable-collection copiers, short-circuiting to a shallow copy when the element copiers are shallow-copyable.
  • Frozen collection types are abstract base types whose runtime instances are internal subtypes, potentially of a different generic arity. The copiers therefore implement IDerivedTypeCopier, and CodecProvider.CreateCopierInstance now advances the field type and search type in lockstep when resolving derived-type copiers, mirroring the existing codec-side logic. Without this, resolving a copier for a subtype with mismatched arity threw an ArgumentException from MakeGenericType.

Tests cover round-tripping through the codec, copier behavior (including mutable element types), and comparer preservation.

Microsoft Reviewers: Open in CodeFlow