@@ -14,6 +14,11 @@ namespace Microsoft.AspNetCore.Components;
1414
1515internal sealed class ComponentFactory
1616{
17+ // This switch is unsupported and will be removed in a future version.
18+ private static readonly bool _propertyInjectionDisabled =
19+ AppContext . TryGetSwitch ( "Microsoft.AspNetCore.Components.Unsupported.DisablePropertyInjection" , out var isDisabled ) &&
20+ isDisabled ;
21+
1722 private const BindingFlags _injectablePropertyBindingFlags
1823 = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
1924
@@ -82,15 +87,18 @@ public IComponent InstantiateComponent(IServiceProvider serviceProvider, [Dynami
8287 throw new InvalidOperationException ( $ "The component activator returned a null value for a component of type { componentType . FullName } .") ;
8388 }
8489
85- if ( component . GetType ( ) == componentType )
86- {
87- // Fast, common case: use the cached data we already looked up
88- propertyInjector ( serviceProvider , component ) ;
89- }
90- else
90+ if ( ! _propertyInjectionDisabled )
9191 {
92- // Uncommon case where the activator/resolver returned a different type. Needs an extra cache lookup.
93- PerformPropertyInjection ( serviceProvider , component ) ;
92+ if ( component . GetType ( ) == componentType )
93+ {
94+ // Fast, common case: use the cached data we already looked up
95+ propertyInjector ( serviceProvider , component ) ;
96+ }
97+ else
98+ {
99+ // Uncommon case where the activator/resolver returned a different type. Needs an extra cache lookup.
100+ PerformPropertyInjection ( serviceProvider , component ) ;
101+ }
94102 }
95103
96104 return component ;
0 commit comments