Skip to content

Optimize codegen for generated dependency property setters#792

Open
Sergio0694 wants to merge 4 commits intomainfrom
user/sergiopedri/optimize-dp-codegen
Open

Optimize codegen for generated dependency property setters#792
Sergio0694 wants to merge 4 commits intomainfrom
user/sergiopedri/optimize-dp-codegen

Conversation

@Sergio0694
Copy link
Copy Markdown
Member

This pull request introduces an optimization to the DependencyPropertyGenerator by leveraging specialized XamlBindingHelper.SetPropertyFrom* methods for supported property types. This results in more efficient generated code for property setters, while maintaining compatibility with user-defined callbacks and fallback logic for unsupported types. The changes also refactor and extend the generator’s internal APIs to support this feature.

Optimized setter generation using XamlBindingHelper:

  • The generator now detects if a property type supports a specialized XamlBindingHelper.SetPropertyFrom* method (e.g., SetPropertyFromBoolean, SetPropertyFromInt32). If so, it emits a direct call to this method in the generated setter for improved performance. This optimization is skipped if the property type is object or if the user has implemented a custom On<PropertyName>Set(ref object) callback. [1] [2] [3] [4]

API and model extensions to support the optimization:

  • Added a new XamlBindingHelperSetMethodName property to the DependencyPropertyInfo record, and updated the generator pipeline to compute and pass this value. This enables the code generation logic to conditionally emit the optimized setter code. [1] [2] [3]

Well-known type name utilities:

  • Introduced a utility method XamlBindingHelper(bool useWindowsUIXaml) in WellKnownTypeNames.cs to resolve the fully qualified type name for XamlBindingHelper, supporting both Windows and Microsoft UI XAML namespaces.

Code generation logic updates:

  • Modified the setter and partial method generation logic to use the optimized path when available, and to only emit the boxed On<PropertyName>Set(ref object) partial method when the optimization is not used. [1] [2] [3] [4] [5]

These changes improve the efficiency of generated dependency property code and maintain backward compatibility for advanced scenarios.

@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from d4c7c8d to 6a789f1 Compare April 14, 2026 21:13
Sergio0694 and others added 2 commits April 14, 2026 16:49
Add support for using XamlBindingHelper.SetPropertyFrom* methods to optimize generated dependency property setters. Introduces WellKnownTypeNames.XamlBindingHelper to resolve the fully-qualified type name, adds Execute.GetXamlBindingHelperSetMethodName and Execute.IsObjectSetCallbackImplemented to detect applicable types and user overrides, and wires the chosen method into the generator pipeline. Generation now emits direct XamlBindingHelper calls when available and falls back to boxed SetValue path otherwise. Also update DependencyPropertyInfo to carry the XamlBindingHelperSetMethodName.
Replace manual boxing and SetValue calls with global::Windows.UI.Xaml.Markup.XamlBindingHelper.SetPropertyFrom* calls across the dependency property generator tests. Update DataRow entries to include the target set method name and refactor the test helper to compute the setter body based on an optional setMethodName parameter. Add a new test (SingleProperty_Int32_WithNoCaching_WithObjectSetCallback) covering an int property with an object-based OnNumberSet callback to validate generated code that mixes object set callbacks and optimized XamlBindingHelper setters.

Co-Authored-By: Copilot <223556219+Copilot@users.noreply.github.com>
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from 6a789f1 to 5187a35 Compare April 14, 2026 23:52
Replace manual boxing and SetValue call in the generated Name property setter with a call to global::Windows.UI.Xaml.Markup.XamlBindingHelper.SetPropertyFromString(this, NameProperty, value) to avoid temporary object boxing and better align with XAML binding semantics. Also add a missing [TestMethod] attribute to the SingleProperty_Int32_WithNoCaching_WithDefaultValue_WithCallback test so it will be executed.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/optimize-dp-codegen branch from 5187a35 to f6cda27 Compare April 14, 2026 23:53
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