Expression.Lambda(exp, parameters);
它完全适用于 JIT。对于 AOT,它使用 <= 15 个参数。
对于 16 个参数,抛出异常如下:
System.NotImplementedException: byref delegate
at System.Linq.Expressions.Interpreter.LightLambda.CreateCustomDelegate (System.Type delegateType) <0x1052b3620 + 0x00738> in <b17c8f8ec0e94e6aa946d54abe0cfd86#d5047ea47da6b88a294b7510b13ff6ef>:0
at System.Linq.Expressions.Interpreter.LightLambda.MakeDelegate (System.Type delegateType) <0x1052b3da0 + 0x0005f> in <b17c8f8ec0e94e6aa946d54abe0cfd86#d5047ea47da6b88a294b7510b13ff6ef>:0
at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate (System.Runtime.CompilerServices.IStrongBox[] closure) <0x1052ac220 + 0x0006f> in <b17c8f8ec0e94e6aa946d54abe0cfd86#d5047ea47da6b88a294b7510b13ff6ef>:0
对于 >= 17 个参数,抛出异常如下:
System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Linq.Expressions.Compiler.DelegateHelpers.MakeNewCustomDelegate (System.Type[] types) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Compiler.DelegateHelpers.MakeNewDelegate (System.Type[] types) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Compiler.DelegateHelpers.MakeDelegateType (System.Type[] types) [0x00000] in <00000000000000000000000000000000>:0
at System.Linq.Expressions.Expression.Lambda (System.Linq.Expressions.Expression body, System.String name, System.Boolean tailCall, System.Collections.Generic.IEnumerable`1[T] parameters) [0x00000] in <00000000000000000000000000000000>:0
我知道 AOT 不应该在运行时编译任何表达式,它会影响性能,因为有一个回退过程。所以我不是在问最好的方法,只是想弄清楚为什么当参数太多但在 AOT 中使用 <= 15 个参数时会引发异常。