Skip to content

Comments

Emit full "Impl" types for IDictionary<TKey, TValue> in 'cswinrtgen'#2173

Merged
Sergio0694 merged 13 commits intostaging/3.0from
user/sergiopedri/dictionary-impl
Jan 17, 2026
Merged

Emit full "Impl" types for IDictionary<TKey, TValue> in 'cswinrtgen'#2173
Sergio0694 merged 13 commits intostaging/3.0from
user/sergiopedri/dictionary-impl

Conversation

@Sergio0694
Copy link
Member

Title. Also added some optimizations for string keys.

Examples of codegen:

image

Note

Draft because there's some commits to move and this branch also needs to be rebased.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements full "Impl" type generation for IDictionary<TKey, TValue> in the cswinrtgen code generator, enabling proper marshaling of dictionary types between managed and unmanaged code. It includes optimizations for string keys to avoid unnecessary allocations by using ReadOnlySpan<char> lookup methods.

Key changes:

  • Added adapter classes (IDictionaryAdapter and IDictionaryAdapterExtensions) for marshaling dictionary operations
  • Implemented all required vtable methods (Lookup, Size, HasKey, GetView, Insert, Remove, Clear) for dictionary impl types
  • Added string-optimized overloads using ReadOnlySpan<char> to avoid allocations

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
IDictionaryAdapter{TKey, TValue}.cs New adapter for marshaling IDictionary<TKey, TValue> operations to WinRT IMap interface
IDictionaryAdapterExtensions.cs String-optimized extension methods using ReadOnlySpan<char> for dictionary lookups
InteropReferences.cs Added references to new adapter types and methods for code generation
InteropMethodDefinitionFactory.IReadOnlyDictionary2Impl.cs Fixed TODO comments and updated to use correct dictionary adapter methods
InteropMethodDefinitionFactory.IDictionary2Impl.cs New factory methods generating impl type vtable methods for IDictionary<TKey, TValue>
InteropTypeDiscovery.Generics.cs Added tracking for IReadOnlyDictionary<TKey, TValue> and ReadOnlyDictionary<TKey, TValue> dependencies
InteropTypeDefinitionBuilder.cs Uncommented vtable validation check
InteropTypeDefinitionBuilder.IReadOnlyDictionary2.cs Minor cleanup and parameter name correction
InteropTypeDefinitionBuilder.IDictionary2.cs Implemented full vtable method generation for dictionary impl types
InteropGeneratorDiscoveryState.cs Fixed comment reference to renamed method

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/read-only-dictionary-impl branch from cb0df78 to 5b21c97 Compare January 16, 2026 23:37
Base automatically changed from user/sergiopedri/read-only-dictionary-impl to staging/3.0 January 17, 2026 00:32
Sergio0694 and others added 13 commits January 16, 2026 16:38
Introduces IDictionaryAdapter<TKey, TValue> as a stateless adapter for IDictionary to expose Windows.Foundation.Collections.IMap functionality. Adds IDictionaryAdapterExtensions for string-keyed dictionaries to optimize lookup, removal, and key existence checks using ReadOnlySpan<char> to avoid string allocations.
Introduces support for generating the 'Lookup' method for IDictionaryAdapter<TKey, TValue> in the IReadOnlyDictionary2 implementation. Adds new type and member references for IDictionaryAdapter2 and its Lookup method, and updates the method factory to use these references for non-string key types.
Introduces the definition and vtable inclusion of the 'get_Size' method for IDictionary2 implementations in the interop type builder. Adds a new helper in InteropReferences to retrieve the MemberReference for IDictionaryAdapter2.Size, supporting correct method generation for COM interop.
Introduces the HasKey method to the IDictionary2 implementation and updates related method signatures and references for consistency. Also corrects parameter naming from 'hasKeyMethod' to 'containsKeyMethod' and adds a new method reference for IDictionaryAdapterOfStringHasKey in InteropReferences.
Introduces a new interop method definition for the GetView method on IDictionary<TKey, TValue> implementations. Updates type discovery to track IReadOnlyDictionary and ReadOnlyDictionary types needed for marshalling GetView results. Adds required references and method factories to support this functionality.
Eliminated unnecessary using statements for System.Collections and System.Diagnostics to clean up the code and improve maintainability.
Updated the XML comment to reference TryMarkUserDefinedType instead of the incorrect MarkUserDefinedType, improving documentation accuracy.
Introduces the Insert method implementation for IDictionary2 interop types by defining the method in InteropMethodDefinitionFactory, wiring it into InteropTypeDefinitionBuilder, and adding the necessary reference in InteropReferences. This enables proper handling of Insert operations for projected IDictionary2 types.
Implements the 'Remove' method for IDictionary2 interop type definitions, including method creation in InteropMethodDefinitionFactory and reference resolution in InteropReferences. This enables proper COM interface generation for dictionary removal operations.
Introduces a Clear method to the IDictionary2 interop type builder and its method factory. This enables support for clearing dictionary instances in the generated interop code.
Uncommented the check to enforce that all vtable entries are initialized by throwing an ArgumentOutOfRangeException if the counts do not match.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Renamed 'hasKeymethod' to 'hasKeyMethod' in both IDictionary2 and IReadOnlyDictionary2 interop builders for consistency and clarity.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/dictionary-impl branch from ed367c1 to d6b161c Compare January 17, 2026 00:38
@Sergio0694 Sergio0694 marked this pull request as ready for review January 17, 2026 00:38
@Sergio0694 Sergio0694 merged commit c774474 into staging/3.0 Jan 17, 2026
10 checks passed
@Sergio0694 Sergio0694 deleted the user/sergiopedri/dictionary-impl branch January 17, 2026 01:32
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.

2 participants