-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[C] Fix NullReferenceException when binding to Dictionary<Enum, object> with x:DataType #32916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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 fixes a NullReferenceException that occurred when using x:DataType with bindings to Dictionary<Enum, object> properties in XAML. The issue was that both XamlC and SourceGen did not support enum types as dictionary indexer parameters, causing compilation failures.
Key Changes:
- Added enum indexer parameter support to XamlC's SetPropertiesVisitor
- Added enum indexer parameter support to SourceGen's CompiledBindingMarkup
- Added comprehensive tests for both XamlC and SourceGen to prevent regression
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs | Added logic to detect enum indexer parameters, validate enum types, and emit enum constant values as IL instructions |
| src/Controls/src/SourceGen/CompiledBindingMarkup.cs | Added logic to detect enum indexer parameters and generate fully qualified enum member references |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui13856.xaml | Test XAML file demonstrating Dictionary<Enum, object> binding with x:DataType |
| src/Controls/tests/Xaml.UnitTests/Issues/Maui13856.xaml.cs | XamlC unit test verifying compilation succeeds without NullReferenceException |
| src/Controls/tests/SourceGen.UnitTests/Maui13856Tests.cs | SourceGen unit test verifying no errors are generated for enum indexer bindings |
| src/Controls/tests/SourceGen.UnitTests/Maui32879Tests.cs | Updated expected test output to include CS0219 pragma warning (pre-existing test fix) |
| if (enumMember != null) | ||
| { | ||
| index = $"{enumType.ToFQDisplayString()}.{indexArg}"; | ||
| } |
Copilot
AI
Nov 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an enum indexer is found but the enum member doesn't exist (enumMember is null), the code silently continues without updating the index variable. This means it will use the original indexArg string value instead of a fully qualified enum member reference, which could lead to incorrect code generation.
Consider adding an else clause to handle the case when the enum member is not found:
if (enumMember != null)
{
index = $"{enumType.ToFQDisplayString()}.{indexArg}";
}
else
{
// Log a diagnostic or return false to indicate parsing failure
return false;
}| } | |
| } | |
| else | |
| { | |
| return false; // Enum member not found, fail parsing | |
| } |
d696d69 to
58e7fbd
Compare
…ith x:DataType Fixes #13856 ## Problem Using with a binding to like caused a NullReferenceException at compile time in XamlC. ## Solution - **XamlC (SetPropertiesVisitor.cs)**: Added support for finding indexers with enum parameter types and emitting enum constant values via IL - **SourceGen (CompiledBindingMarkup.cs)**: Added support for enum indexer parameter types in the binding path parser ## Changes - Added enum indexer detection in XamlC's property digger - Updated type validation to allow enum indexer types - Added code to emit enum constant values as IL instructions - Added enum indexer support in SourceGen's TryParsePath method - Fixed pre-existing test failure in Maui32879Tests (unrelated pragma warning directive) ## Tests - Added Maui13856.xaml unit test for XamlC compilation - Added Maui13856Tests.cs for SourceGen validation
58e7fbd to
6f6407e
Compare
| // Verify the binding path is in the generated code (even if using runtime binding fallback) | ||
| Assert.Contains("UserSettings[TBD]", generatedCode, System.StringComparison.Ordinal); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this assert should be here. Ideally we'll drop the unnecessary markup extension instantiation at some point (#31614). I think this assert should look for .UserSettings[global::Test.UserSetting.TBD] instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(although that might currently fail due to #32905)
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Description
Fixes #13856
Problem
Using
x:DataTypewith a binding toDictionary<CustomEnum, object>like{Binding UserSettings[TBD]}caused a NullReferenceException at compile time in XamlC.Root Cause
XamlC's
SetPropertiesVisitor.csonly supportedint,string, andobjectindexer parameter types. When encountering an enum type, it failed to find a matching indexer and returnednull, causing a NullReferenceException when trying to access properties on the null indexer.Solution
Changes
SetPropertiesVisitor.cs
CompiledBindingMarkup.cs
TryParsePathmethodTest Fixes
Maui32879Tests.cs(updated expected output to include#pragma warning disable CS0219)Tests Added
Maui13856.xaml+Maui13856.xaml.cs- XamlC unit test that verifies compilation succeedsMaui13856Tests.cs- SourceGen unit test that verifies no errors are generatedTest Results