From 7909934a4c7da7b62d7a7496788896ab5c072b44 Mon Sep 17 00:00:00 2001 From: Javier Calvarro Nelson Date: Mon, 1 Sep 2025 13:40:17 +0200 Subject: [PATCH] Add unsupported compat switch for property injection --- .../Components/src/ComponentFactory.cs | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/Components/Components/src/ComponentFactory.cs b/src/Components/Components/src/ComponentFactory.cs index 5a6c5b5c71c8..0618c9a4d20a 100644 --- a/src/Components/Components/src/ComponentFactory.cs +++ b/src/Components/Components/src/ComponentFactory.cs @@ -14,6 +14,11 @@ namespace Microsoft.AspNetCore.Components; internal sealed class ComponentFactory { + // This switch is unsupported and will be removed in a future version. + private static readonly bool _propertyInjectionDisabled = + AppContext.TryGetSwitch("Microsoft.AspNetCore.Components.Unsupported.DisablePropertyInjection", out var isDisabled) && + isDisabled; + private const BindingFlags _injectablePropertyBindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; @@ -82,15 +87,18 @@ public IComponent InstantiateComponent(IServiceProvider serviceProvider, [Dynami throw new InvalidOperationException($"The component activator returned a null value for a component of type {componentType.FullName}."); } - if (component.GetType() == componentType) - { - // Fast, common case: use the cached data we already looked up - propertyInjector(serviceProvider, component); - } - else + if (!_propertyInjectionDisabled) { - // Uncommon case where the activator/resolver returned a different type. Needs an extra cache lookup. - PerformPropertyInjection(serviceProvider, component); + if (component.GetType() == componentType) + { + // Fast, common case: use the cached data we already looked up + propertyInjector(serviceProvider, component); + } + else + { + // Uncommon case where the activator/resolver returned a different type. Needs an extra cache lookup. + PerformPropertyInjection(serviceProvider, component); + } } return component;