1+ using System . Collections . Concurrent ;
12using System . Diagnostics ;
23using System . Reflection ;
34using System . Runtime . CompilerServices ;
@@ -13,6 +14,7 @@ public sealed class BunitRenderer : Renderer
1314{
1415 private readonly BunitServiceProvider services ;
1516 private readonly List < Task > disposalTasks = [ ] ;
17+ private static readonly ConcurrentDictionary < Type , ConstructorInfo > componentActivatorCache = new ( ) ;
1618
1719 [ UnsafeAccessor ( UnsafeAccessorKind . Field , Name = "_isBatchInProgress" ) ]
1820 private static extern ref bool GetIsBatchInProgressField ( Renderer renderer ) ;
@@ -214,11 +216,28 @@ protected override ComponentState CreateComponentState(int componentId, ICompone
214216
215217 var TComponent = component . GetType ( ) ;
216218 var renderedComponentType = typeof ( RenderedComponent < > ) . MakeGenericType ( TComponent ) ;
217- var renderedComponent = Activator . CreateInstance ( renderedComponentType , this , componentId , component , services , parentComponentState ) ;
219+ var renderedComponent = CreateComponentInstance ( ) ;
218220
219221 Debug . Assert ( renderedComponent is not null , "RenderedComponent should not be null" ) ;
220222
221223 return ( ComponentState ) renderedComponent ;
224+
225+ object CreateComponentInstance ( )
226+ {
227+ var constructorInfo = componentActivatorCache . GetOrAdd ( renderedComponentType , type
228+ => type . GetConstructor (
229+ [
230+ typeof ( BunitRenderer ) ,
231+ typeof ( int ) ,
232+ typeof ( IComponent ) ,
233+ typeof ( IServiceProvider ) ,
234+ typeof ( ComponentState )
235+ ] ) ! ) ;
236+
237+ Debug . Assert ( constructorInfo is not null , "Could not find ConstructorInfo" ) ;
238+
239+ return constructorInfo . Invoke ( [ this , componentId , component , services , parentComponentState ] ) ;
240+ }
222241 }
223242
224243 /// <inheritdoc/>
0 commit comments