Skip to content

Fix optimized DP setter codegen for 'string' properties#794

Open
Sergio0694 wants to merge 1 commit intomainfrom
user/sergiopedri/fix-string-codegen
Open

Fix optimized DP setter codegen for 'string' properties#794
Sergio0694 wants to merge 1 commit intomainfrom
user/sergiopedri/fix-string-codegen

Conversation

@Sergio0694
Copy link
Copy Markdown
Member

Follow-up to #792.

The optimized codegen for generated dependency property setters introduced in #792 calls XamlBindingHelper.SetProperty* directly, but it turns out that XamlBindingHelper.SetPropertyFromString does not work correctly when the value is null or an empty string. To work around this, the generator now special-cases string properties and emits this code instead of an unconditional helper call:

if (value is null || value.Length == 0)
{
    SetValue(TestProperty, value);
}
else
{
    XamlBindingHelper.SetPropertyFromString(this, TestProperty, value);
}

This is applied to both the local-caching and no-caching setter paths.

The existing unit tests covering string and string? dependency properties (including the parameterized rows in SingleProperty_MultipleTypes_WithNoCaching_DefaultValueIsOptimized and SingleProperty_WithCustomMetadataType_WithNoCaching, plus the explicit SingleProperty_String_WithLocalCache test and others) have been updated to reflect the new generated code, so coverage for both setter paths is preserved.

XamlBindingHelper.SetPropertyFromString does not handle 'null' or empty strings correctly, so for 'string' typed dependency properties, fall back to SetValue in those cases and only call the optimized helper for non-empty strings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 requested a review from Arlodotexe April 22, 2026 04:20
{
writer.Write($$"""

if (value is null || value.Length == 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
if (value is null || value.Length == 0)
if (string.IsNullOrEmpty(value))

QQ: I know it's equivalent but would using the string helper be clearer/more efficient in anyway? (i.e. would it be optimized by the compiler anyway vs. the function overhead?)

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