1
+ using System . Collections . Concurrent ;
1
2
using System . Diagnostics ;
2
3
using System . Reflection ;
3
4
using System . Runtime . CompilerServices ;
@@ -13,6 +14,7 @@ public sealed class BunitRenderer : Renderer
13
14
{
14
15
private readonly BunitServiceProvider services ;
15
16
private readonly List < Task > disposalTasks = [ ] ;
17
+ private static readonly ConcurrentDictionary < Type , ConstructorInfo > componentActivatorCache = new ( ) ;
16
18
17
19
[ UnsafeAccessor ( UnsafeAccessorKind . Field , Name = "_isBatchInProgress" ) ]
18
20
private static extern ref bool GetIsBatchInProgressField ( Renderer renderer ) ;
@@ -214,11 +216,28 @@ protected override ComponentState CreateComponentState(int componentId, ICompone
214
216
215
217
var TComponent = component . GetType ( ) ;
216
218
var renderedComponentType = typeof ( RenderedComponent < > ) . MakeGenericType ( TComponent ) ;
217
- var renderedComponent = Activator . CreateInstance ( renderedComponentType , this , componentId , component , services , parentComponentState ) ;
219
+ var renderedComponent = CreateComponentInstance ( ) ;
218
220
219
221
Debug . Assert ( renderedComponent is not null , "RenderedComponent should not be null" ) ;
220
222
221
223
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
+ }
222
241
}
223
242
224
243
/// <inheritdoc/>
0 commit comments