Port Keys/Values dictionary codegen fix to the C# projection writer#2435
Open
Sergio0694 wants to merge 2 commits into
Open
Port Keys/Values dictionary codegen fix to the C# projection writer#2435Sergio0694 wants to merge 2 commits into
Sergio0694 wants to merge 2 commits into
Conversation
Ports the 'cswinrt/code_writers.h' fix from PR #2427 to the C# projection writer, for the 'IMapView<K,V>' -> 'IReadOnlyDictionary<K,V>' stub path. Three changes to the emitted 'Keys'/'Values' members: * The '[UnsafeAccessor]' for 'Keys'/'Values' now returns 'IEnumerable<T>' (was 'ICollection<T>'), matching the actual signature of the interop helper 'IReadOnlyDictionaryMethods.Keys/Values' (the public property was already 'IEnumerable<T>', so the accessor return type was a latent mismatch that happened to bind to the wrong contract). * The accessor receiver is now 'WindowsRuntimeObject windowsRuntimeObject' (the projected runtime class itself, passed as 'this') instead of the interface 'WindowsRuntimeObjectReference objRef'. * The public 'Keys'/'Values' properties cache the returned collection via the C# 14 'field' keyword ('=> field ??= ...(null, this)'), so repeated accesses preserve reference identity instead of allocating a fresh wrapper each time. To support a non-default receiver, 'EmitUnsafeAccessor' gains an optional 'receiver' parameter (defaulting to 'WindowsRuntimeObjectReference objRef'); 'Keys'/'Values' are emitted via direct calls (with the 'WindowsRuntimeObject' receiver) ahead of the remaining accessors, which continue to flow through the table-based 'EmitUnsafeAccessors'. The interop-generator and unit-test parts of #2427 are intentionally not included here; they arrive when this branch merges back into staging/3.0. Verified by generating the full 'Windows.winmd' reference projection and inspecting the emitted 'IReadOnlyDictionaryMethods' stubs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Ports the 'cswinrt/code_writers.h' fix from PR #2427 to the C# projection writer, for the 'IMap<K,V>' -> 'IDictionary<K,V>' stub path. Mirrors the 'IReadOnlyDictionary' change in the previous commit (the return type stays 'ICollection<T>' here, matching the interop helper 'IDictionaryMethods.Keys/Values'): * The '[UnsafeAccessor]' for 'Keys'/'Values' now takes the projected runtime class ('WindowsRuntimeObject windowsRuntimeObject', passed as 'this') instead of the interface 'WindowsRuntimeObjectReference objRef'. * The public 'Keys'/'Values' properties cache the returned collection via the C# 14 'field' keyword ('=> field ??= ...(null, this)'), preserving reference identity across accesses. 'Keys'/'Values' are emitted via direct 'EmitUnsafeAccessor' calls (with the 'WindowsRuntimeObject' receiver) ahead of the remaining accessors, which continue to flow through the table-based 'EmitUnsafeAccessors'. Verified by generating the full 'Windows.winmd' reference projection: the emitted 'IDictionaryMethods' stubs match the expected output, the run is deterministic (347/347 files, 0 diffs across two runs), and the 'WindowsRuntimeObject' receiver appears only on dictionary 'Keys'/'Values' (all other collection accessors keep the 'WindowsRuntimeObjectReference' receiver). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
manodasanW
reviewed
Jun 12, 2026
| string enumerableObjRefName = "_objRef_System_Collections_Generic_IEnumerable_" + IidExpressionGenerator.EscapeTypeNameForIdentifier(kvLong, stripGlobal: false) + "_"; | ||
|
|
||
| writer.WriteLine(); | ||
|
|
Member
There was a problem hiding this comment.
does this branch have the tests you added in the other PR?
Member
Author
There was a problem hiding this comment.
Not yet, I need to merge staging/3.0 into this staging still.
manodasanW
approved these changes
Jun 12, 2026
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.
Summary
Ports the projection code-generation fix from merged PR #2427 (
Fix missing Keys/Values on projected dictionary types) from the C++ generator (src/cswinrt/code_writers.h) to the C# projection writer. Only the projection writer is changed here; the interop-generator and unit-test parts of #2427 arrive when this branch is merged back intostaging/3.0.Motivation
PR #2427 fixed the projected
Keys/Valuesmembers on dictionary types (IMap<K,V>->IDictionary<K,V>andIMapView<K,V>->IReadOnlyDictionary<K,V>). Since the C# projection writer is a port of the C++cswinrtcode writers, it needs the same fix so it emits identical stubs. Without it, the C# writer would emitKeys/Valuesaccessors that bind to the wrong interop contract and re-allocate a fresh collection wrapper on every access.Changes
src/WinRT.Projection.Writer/Factories/MappedInterfaceStubFactory.cs: ported the fix to both dictionary stub emitters:Keys/Values[UnsafeAccessor]declarations now take the projected runtime class (WindowsRuntimeObject windowsRuntimeObject, passed asthis) instead of the interface object reference (WindowsRuntimeObjectReference objRef), matching the interop helper signatures introduced by Fix missing Keys/Values on projected dictionary types #2427.Keys/Valuesaccessor return type is corrected fromICollection<T>toIEnumerable<T>to match the actualIReadOnlyDictionaryMethods.Keys/Valuescontract (the public property was alreadyIEnumerable<T>, so the accessor return type was a latent mismatch).Keys/Valuesproperties now cache the returned collection via the C# 14fieldkeyword (=> field ??= ...(null, this)), preserving reference identity across repeated accesses.EmitUnsafeAccessorgains an optionalreceiverparameter (defaulting toWindowsRuntimeObjectReference objRef) so the dictionaryKeys/Valuesaccessors can opt into theWindowsRuntimeObjectreceiver while every other accessor is unchanged.Validation
Windows.winmdreference projection (347 files): bothIDictionaryMethodsandIReadOnlyDictionaryMethodsstubs match the expected output, and the newWindowsRuntimeObjectreceiver appears only on dictionaryKeys/Values(all other collection accessors keep theWindowsRuntimeObjectReferencereceiver).