Skip to content

[XC] add IRootObjectProvider #28310

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

Merged
merged 2 commits into from
Apr 11, 2025
Merged

[XC] add IRootObjectProvider #28310

merged 2 commits into from
Apr 11, 2025

Conversation

StephaneDelcroix
Copy link
Contributor

@StephaneDelcroix StephaneDelcroix commented Mar 11, 2025

Description of Change

[XC] add IRootObjectProvider

this was never meant to be public, but it is. and people are using it

Issues Fixed

@Copilot Copilot AI review requested due to automatic review settings March 11, 2025 14:21
@StephaneDelcroix StephaneDelcroix requested a review from a team as a code owner March 11, 2025 14:21
Copy link
Contributor

@Copilot Copilot AI left a 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 introduces the IRootObjectProvider into several components to address its unintended public exposure and to properly handle XAML type resolution and IL generation.

  • Integrates IRootObjectProvider into XAML service provider classes.
  • Updates array type resolution logic in XamlCache.cs.
  • Adjusts test cases and IL generation in NodeILExtensions.cs to verify and support the new provider.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

File Description
src/Controls/src/Build.Tasks/XamlCache.cs Enhanced GetOrAddTypeReference to handle array types.
src/Controls/tests/Xaml.UnitTests/ServiceProviderTests.xaml.cs Modified test output and attributes to validate changes for IRootObjectProvider.
src/Controls/src/Xaml/XamlServiceProvider.cs Extended SimpleValueTargetProvider to implement IRootObjectProvider.
src/Controls/src/Build.Tasks/NodeILExtensions.cs Updated IL generation to load and register the root object and IRootObjectProvider.
Comments suppressed due to low confidence (4)

src/Controls/tests/Xaml.UnitTests/ServiceProviderTests.xaml.cs:25

  • Ensure that the dynamic string content for IRootObjectProvider remains consistent and expected, especially if type names might vary across different contexts.
services.Add($"IRootObjectProvider({((IRootObjectProvider)serviceProvider.GetService(typeof(IRootObjectProvider))).RootObject.GetType().Name})")

src/Controls/tests/Xaml.UnitTests/ServiceProviderTests.xaml.cs:71

  • Verify that the revised test attribute setup ([Test] with [Values]) adequately covers the intended test cases, as the change from [TestCase(true)] might affect test variations.
[Test] public void TestServiceProviders([Values]bool useCompiledXaml)

src/Controls/src/Build.Tasks/NodeILExtensions.cs:649

  • Ensure that context.Root reliably returns a VariableDefinition or FieldReference; otherwise, the IL generation will fall back to loading null, which might lead to unintended behavior.
if (context.Root is VariableDefinition rootVariable)

src/Controls/src/Build.Tasks/NodeILExtensions.cs:684

  • Confirm that duplicating the serviceProvider on the stack does not inadvertently affect the instruction order or subsequent IL operations, potentially leading to runtime issues.
yield return Create(Dup); //Keep the serviceProvider on the stack

@@ -135,10 +135,11 @@ public ValueTargetProvider(object targetObject, object targetProperty)
}
#nullable restore

public class SimpleValueTargetProvider : IProvideParentValues, IProvideValueTarget, IReferenceProvider
public class SimpleValueTargetProvider : IProvideParentValues, IProvideValueTarget, IReferenceProvider, IRootObjectProvider
Copy link
Preview

Copilot AI Mar 11, 2025

Choose a reason for hiding this comment

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

Adding IRootObjectProvider to a public class constitutes a breaking change in the public API; please ensure that this change is well-documented and validated in downstream usage.

Copilot uses AI. Check for mistakes.

this was never meant to be public, but it was. and people are using it

- fixes #16881
@@ -38,8 +39,13 @@ public TypeDefinition Resolve(TypeReference typeReference) =>
public FieldReference GetOrAddFieldReference((ModuleDefinition module, string fieldRefKey) key, Func<(ModuleDefinition module, string fieldRefKey), FieldReference> valueFactory) =>
GetOrAdd(_fieldReferenceCache, key, valueFactory);

public TypeReference GetOrAddTypeReference(ModuleDefinition module, (string assemblyName, string clrNamespace, string typeName) type) =>
GetOrAdd(_typeReferenceCache, (module, type.ToString()), x => x.module.ImportReference(x.module.GetTypeDefinition(this, type)));
public TypeReference GetOrAddTypeReference(ModuleDefinition module, (string assemblyName, string clrNamespace, string typeName) type) => GetOrAdd(_typeReferenceCache, (module, type.ToString()), x =>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this allows specifying [] in the string tuple for type, so we can pick a methodreference with the right parameters if some of them are arrays

@@ -147,7 +148,13 @@ public SimpleValueTargetProvider(object[] objectAndParents, object targetPropert
{
}

[Obsolete("Use the other ctor")]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we need to keep this around, like the other one, to make code compiled with a previous version of XamlC to keep working

}
else
yield return Create(Ldnull);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the runtime cost of passing an extra parameter that doesn't need to be evaluated is negligible

@jfversluis jfversluis added the area-xaml XAML, CSS, Triggers, Behaviors label Mar 25, 2025
@@ -8,3 +8,5 @@ Microsoft.Maui.Controls.Embedding.EmbeddingExtensions
Microsoft.Maui.Controls.Xaml.Internals.ValueTargetProvider
Microsoft.Maui.Controls.Xaml.Internals.ValueTargetProvider.ValueTargetProvider(object! targetObject, object! targetProperty) -> void
Microsoft.Maui.Controls.Xaml.ResourceDictionaryHelpers
~Microsoft.Maui.Controls.Xaml.Internals.SimpleValueTargetProvider.RootObject.get -> object
Copy link
Contributor

Choose a reason for hiding this comment

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

Is not modifying the publicAPI shipped files, so, is fine to include it in a SR?

GetOrAdd(_typeReferenceCache, (module, type.ToString()), x => x.module.ImportReference(x.module.GetTypeDefinition(this, type)));
public TypeReference GetOrAddTypeReference(ModuleDefinition module, (string assemblyName, string clrNamespace, string typeName) type) => GetOrAdd(_typeReferenceCache, (module, type.ToString()), x =>
{
if (type.typeName.EndsWith("[]"))
Copy link
Contributor

Choose a reason for hiding this comment

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

While type.typeName.EndsWith("[]") works, EndsWith internally traverses the string. Not sure if we have types with less than 2 characters but, consider checking its length first:

if (type.typeName.Length >= 2 && type.typeName.EndsWith("[]"))

Copy link
Contributor Author

@StephaneDelcroix StephaneDelcroix Mar 28, 2025

Choose a reason for hiding this comment

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

this happens at compile time, max once per type (as we cache the results), so I won't even bother profiling the potential gain with the 26^2 (a bit more) existing names of length 2 :)

@@ -147,7 +148,13 @@ public SimpleValueTargetProvider(object[] objectAndParents, object targetPropert
{
}

[Obsolete("Use the other ctor")]
Copy link
Contributor

Choose a reason for hiding this comment

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

Just try to add more details about the suggested alternative:
[Obsolete("This constructor is deprecated. Use the SimpleValueTargetProvider constructor with INameScope[] scopes and object rootObject instead.")]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is only used by generated code, so the obsolete message should never show up

@jfversluis jfversluis added this to the .NET 9 SR7 milestone Apr 10, 2025
@PureWeen PureWeen changed the base branch from main to inflight/current April 11, 2025 15:44
@PureWeen PureWeen merged commit 0fdb9ff into inflight/current Apr 11, 2025
126 of 128 checks passed
@PureWeen PureWeen deleted the fix_16881 branch April 11, 2025 15:44
PureWeen pushed a commit that referenced this pull request Apr 16, 2025
* [XC] add IRootObjectProvider

this was never meant to be public, but it was. and people are using it

- fixes #16881

* ignore case
sheiksyedm pushed a commit to sheiksyedm/maui that referenced this pull request Apr 17, 2025
* [XC] add IRootObjectProvider

this was never meant to be public, but it was. and people are using it

- fixes dotnet#16881

* ignore case
NanthiniMahalingam pushed a commit to NanthiniMahalingam/maui that referenced this pull request Apr 17, 2025
* [XC] add IRootObjectProvider

this was never meant to be public, but it was. and people are using it

- fixes dotnet#16881

* ignore case
github-actions bot pushed a commit that referenced this pull request Apr 18, 2025
* [XC] add IRootObjectProvider

this was never meant to be public, but it was. and people are using it

- fixes #16881

* ignore case
PureWeen added a commit that referenced this pull request Apr 23, 2025
### Description of Change

## .NET MAUI Release Notes - Inflight/Candidate

## What's Changed

### MAUI Product Fixes
* Fix CarouselView layout SR6 regressions by @albyrock87 in
#29035
* Revert "[Android] picker - focus/unfocus events (#28122)" by @PureWeen
in https://github.com/dotnet/maui/pull/1fb5164929
* [Windows] Fixed the flyout content width not being set correctly after
updating to WinUI SDK 1.7 by @Tamilarasan-Paranthaman in
#28996
* [Android] picker - focus/unfocus events by @kubaflo in
#28122
* [XC] add IRootObjectProvider by @StephaneDelcroix in
#28310
* [iOS] Fix for the File.ContentType from MediaPicker not being in valid
MIME format by @SyedAbdulAzeemSF4852 in
#28842
* [Android] Fixed the Incorrect Text Color Applied to Selected Tab in
TabbedPage by @Ahamed-Ali in #28844
* [iOS] Fix FlyoutPage does not respond to changes in the
FlyoutLayoutBehavior property by @devanathan-vaithiyanathan in
#28884
* [Android] Fixed ScalingCanvas.SetBlur not working by
@NirmalKumarYuvaraj in #28911
* [iOS] - Resolved Proper Rendering of Dynamic Header/Footer Updates in
CV2 by @prakashKannanSf3972 in #28641
* [iOS] Fixed the TargetInvocationException Occurs When Selecting
Header/Footer After Changing ItemsLayout in CV2 by @Ahamed-Ali in
#28890
* [Windows] - Fix Visual State Issue with Picker TextColor After
Navigation by @prakashKannanSf3972 in
#28746

### Dependency Updates
* [Windows] Upgrade to Windows App SDK 1.7 by @MartyIX in
#28499

### Testing
* [Testing] Feature Matrix UITest Cases for CollectionView EmptyView
Feature by @NafeelaNazhir in #28679
* Fixed Test case failure in PR 29037 - [2025/04/21] Candidate by
@HarishKumarSF4517 in #29049

**Full Changelog**:
main...inflight/candidate


For more information about inflight process check
https://github.com/dotnet/maui/wiki/Inflight-Branch-Process
@github-actions github-actions bot locked and limited conversation to collaborators May 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-xaml XAML, CSS, Triggers, Behaviors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

MarkupExtension: IRootObjectProvider not available in release mode
4 participants