-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Closed
Copy link
Description
ActivatorUtilities.CreateFactory uses compiled expressions to generate a high-performance factory on CoreCLR:
Lines 126 to 137 in 007df05
| public static ObjectFactory CreateFactory( | |
| [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type instanceType, | |
| Type[] argumentTypes) | |
| { | |
| CreateFactoryInternal(instanceType, argumentTypes, out ParameterExpression provider, out ParameterExpression argumentArray, out Expression factoryExpressionBody); | |
| var factoryLambda = Expression.Lambda<Func<IServiceProvider, object?[]?, object>>( | |
| factoryExpressionBody, provider, argumentArray); | |
| Func<IServiceProvider, object?[]?, object>? result = factoryLambda.Compile(); | |
| return result.Invoke; | |
| } |
NativeAOT doesn't support compiled expressions. Expressions is now a negative:
- Expressions are interpreted, reducing performance compared to standard .NET reflection.
- System.Linq.Expressions usage increases app size
CreateFactory should check for RuntimeFeature.IsDynamicCodeSupported and provide a delegate that uses standard .NET reflection when dynamic code isn't supported.
For example: