Fix empty Dictionary when passing JS object to CLR method accepting Dictionary<string, T>#2312
Merged
Merged
Conversation
…ictionary<string, T> Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix empty dictionary issue in Translatable class
Fix empty Dictionary when passing JS object to CLR method accepting Dictionary<string, T>
Mar 6, 2026
lahma
marked this pull request as ready for review
March 6, 2026 10:32
lahma
approved these changes
Mar 6, 2026
lahma
enabled auto-merge (squash)
March 6, 2026 10:42
This was referenced Mar 7, 2026
This was referenced Jun 8, 2026
This was referenced Jun 29, 2026
This was referenced Jul 7, 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.
When a JavaScript object is passed to a CLR method expecting a concrete
Dictionary<string, T>parameter, the resulting dictionary is always empty.Root Cause
DefaultTypeConverter.TryConverthandles source types that are string-keyed generic dictionaries (e.g.ExpandoObject, which is what plain JS objects become viaToObject()) by iterating the target type's members and looking them up in the source. ForDictionary<string, object>, those members areCount,Keys,Values, etc. — none of which exist as keys in the source, so nothing is populated.This only manifested with concrete dictionary classes. Interface targets like
IDictionary<string, object>worked fine becauseExpandoObjectdirectly implements that interface, so the earliertype.IsInstanceOfType(value)check short-circuits correctly.Fix
When both source and target are string-keyed generic dictionaries, iterate the source's keys and populate the target via
IDictionary, converting each value to the target's declared value type.Changes
DefaultTypeConverter.TryConvert: When sourceTypeDescriptor.IsStringKeyedGenericDictionaryis true and the target type is also a string-keyed generic dictionary (detected viaTypeDescriptor), populate the target dictionary from source keys rather than mapping to target class members. Falls back to the original member-mapping path for non-dictionary targets.InteropTests: AddedTest3/Test4methods and two new tests coveringDictionary<string, object>as a direct parameter and as an optional parameter with anulldefault.Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.