Emit full "Impl" types for IReadOnlyDictionary<TKey, TValue> in 'cswinrtgen'#2172
Merged
Sergio0694 merged 17 commits intostaging/3.0from Jan 17, 2026
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR implements full "Impl" type code generation for IReadOnlyDictionary<TKey, TValue> in the cswinrtgen tool, enabling proper WinRT interop for read-only dictionary types. The implementation includes optimizations for string keys to avoid allocations.
Key changes:
- Added adapter and split adapter classes for
IReadOnlyDictionary<TKey, TValue>to handle WinRT'sIMapView<K, V>interface requirements - Implemented method factories for Lookup, Size, HasKey, and Split operations with string key optimizations
- Removed obsolete
IMapViewVftbl.csas it's replaced by the new implementation
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| IMapViewVftbl.cs | Removed deprecated vtable binding type |
| IReadOnlyDictionarySplitAdapter{TKey, TValue}.cs | Added split adapter supporting sorted dictionary halving for WinRT Split operation |
| IReadOnlyDictionaryAdapter{TKey, TValue}.cs | Added stateless adapter providing core dictionary operations for WinRT projection |
| IReadOnlyDictionaryAdapterExtensions.cs | Added string-key optimizations using alternate lookups to avoid allocations |
| InteropReferences.cs | Added type references for new adapter types and their methods |
| InteropMethodDefinitionFactory.IReadOnlyList1Impl.cs | Updated comments to clarify IndexOf method reusability |
| InteropMethodDefinitionFactory.IReadOnlyDictionary2Impl.cs | Added method factories for all IReadOnlyDictionary impl operations |
| ImportExtensions.cs | Added Import overload for IMethodDescriptor |
| InteropTypeDiscovery.Generics.cs | Added tracking for IReadOnlyDictionarySplitAdapter type instances |
| InteropTypeDefinitionBuilder.IReadOnlyDictionary2.cs | Implemented full Impl type builder with all vtable methods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
manodasanW
approved these changes
Jan 10, 2026
3064471 to
b4c923a
Compare
Deleted the IMapViewVftbl struct from InteropServices Vtables as part of code cleanup or refactoring. This may indicate a change in how IMapView interfaces are handled or a removal of unused interop code.
Introduces IReadOnlyDictionarySplitAdapter<TKey, TValue>, an adapter for IReadOnlyDictionary that maintains keys in sorted order and supports efficient splitting. This class is intended for internal use and is marked obsolete, providing binary search-based lookups and split functionality for Windows Runtime interop scenarios.
Introduces a static adapter class to expose IReadOnlyDictionary<TKey, TValue> as Windows.Foundation.Collections.IMapView<K, V>. Provides Lookup, Size, and Split methods for compatibility with WinRT map view interfaces.
Introduces IReadOnlyDictionaryAdapterExtensions with a Lookup extension method for IReadOnlyDictionary<string, TValue> that accepts ReadOnlySpan<char> keys. This method optimizes lookups by avoiding string allocations when possible, using alternate lookup mechanisms for Dictionary, ConcurrentDictionary, and FrozenDictionary types.
Introduces a new Lookup method implementation for IReadOnlyDictionary2 interop types, including a dedicated factory and supporting references. Updates the builder to use this method in vtable construction and refines parameter rewrite tracking in IReadOnlyList1Impl.
Introduced an Import extension method for IMethodDescriptor to allow importing method descriptors into a module using the default importer. This enhances consistency and usability alongside existing import extensions.
Refactored the Lookup method generation to accept a lookupMethod parameter, enabling optimized handling for string key types and supporting both IReadOnlyDictionary and IDictionary interfaces. Updated related builder and reference code to support this change and added remarks for clarity.
Introduces the get_Size method implementation for IReadOnlyDictionary2 interop types. Updates the builder to include get_Size in the vtable, adds a factory method to generate the method definition, and provides a reference accessor for the Size property in InteropReferences.
Introduces a HasKey method for IReadOnlyDictionary2 interop types, including method generation in the builder and factory, and a fast-path extension for string keys using ReadOnlySpan<char>. This improves performance and feature parity for COM interop scenarios involving IReadOnlyDictionary.
Introduces the Split method to the IReadOnlyDictionary2 interop implementation, including its definition, method body, and reference resolution. This enables support for splitting IReadOnlyDictionary2 instances in the generated interop code.
Adds handling for IReadOnlyDictionarySplitAdapter<TKey, TValue> in InteropTypeDiscovery to support types returned by IMapView<K, V>.Split. Also introduces a TypeReference for IReadOnlyDictionarySplitAdapter2 in InteropReferences.
Replaces InvalidOperationException with KeyNotFoundException when a key is not found in IReadOnlyDictionary adapters. This change improves exception accuracy and aligns with .NET conventions.
Renamed the variable 'getAtMethod' to 'lookupMethod' to better reflect its purpose and improve code readability in the IReadOnlyDictionary2 interop builder.
Applied the MethodImplOptions.AggressiveInlining attribute to the Size method in IReadOnlyDictionaryAdapter to improve performance by suggesting inlining to the compiler.
Corrects the generic parameter signatures used in the IReadOnlyDictionary2 reference for the Lookup method, ensuring the correct type parameters are passed instead of previously used variables.
Adds special handling for ArraySegment<T>.Enumerator returned by IReadOnlyDictionarySplitAdapter<TKey, TValue>.GetEnumerator() in cswinrtgen. Updates InteropReferences to expose ArraySegment1Enumerator and documents the need to keep implementation in sync with code generator logic.
cb0df78 to
5b21c97
Compare
The unnecessary 'using System;' directive was removed from InteropTypeDefinitionBuilder.IReadOnlyDictionary2.cs to clean up unused imports.
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.
Title. Also added some optimizations for
stringkeys.Examples of codegen:
Note
Draft because there's some commits to move and this branch also needs to be rebased.