|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license.
|
3 | 3 |
|
4 | 4 | using System.Diagnostics;
|
| 5 | +using System.Runtime.CompilerServices; |
5 | 6 |
|
6 | 7 | namespace System.Reflection
|
7 | 8 | {
|
8 | 9 | internal sealed partial class ConstructorInvoker
|
9 | 10 | {
|
10 |
| - private readonly RuntimeConstructorInfo _constructorInfo; |
| 11 | + private readonly RuntimeConstructorInfo _method; |
11 | 12 | public InvocationFlags _invocationFlags;
|
| 13 | + private bool _invoked; |
| 14 | + private bool _strategyDetermined; |
| 15 | + private InvokerEmitUtil.InvokeFunc<ConstructorInvoker>? _emitInvoke; |
12 | 16 |
|
13 | 17 | public ConstructorInvoker(RuntimeConstructorInfo constructorInfo)
|
14 | 18 | {
|
15 |
| - _constructorInfo = constructorInfo; |
| 19 | + _method = constructorInfo; |
16 | 20 | }
|
17 | 21 |
|
18 | 22 | [DebuggerStepThrough]
|
19 | 23 | [DebuggerHidden]
|
20 | 24 | public unsafe object? InvokeUnsafe(object? obj, IntPtr* args, Span<object?> argsForTemporaryMonoSupport, BindingFlags invokeAttr)
|
21 | 25 | {
|
22 |
| - // Todo: add strategy for calling IL Emit-based version |
23 |
| - return _constructorInfo.InvokeNonEmitUnsafe(obj, args, argsForTemporaryMonoSupport, invokeAttr); |
| 26 | + if (!_strategyDetermined) |
| 27 | + { |
| 28 | + if (!_invoked) |
| 29 | + { |
| 30 | + // The first time, ignoring race conditions, use the slow path. |
| 31 | + _invoked = true; |
| 32 | + |
| 33 | +#if true |
| 34 | + // TEMP HACK FOR FORCING IL ON FIRST TIME: |
| 35 | + if (RuntimeFeature.IsDynamicCodeCompiled) |
| 36 | + { |
| 37 | + _emitInvoke = InvokerEmitUtil.CreateInvokeDelegate<ConstructorInvoker>(_method); |
| 38 | + } |
| 39 | + _strategyDetermined = true; |
| 40 | +#endif |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + if (RuntimeFeature.IsDynamicCodeCompiled) |
| 45 | + { |
| 46 | + _emitInvoke = InvokerEmitUtil.CreateInvokeDelegate<ConstructorInvoker>(_method); |
| 47 | + } |
| 48 | + |
| 49 | + _strategyDetermined = true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if (_method.SupportsNewInvoke) |
| 54 | + { |
| 55 | + if ((invokeAttr & BindingFlags.DoNotWrapExceptions) == 0) |
| 56 | + { |
| 57 | + try |
| 58 | + { |
| 59 | + // For the rare and broken scenario of calling the constructor directly through MethodBase.Invoke() |
| 60 | + // with a non-null 'obj', we use the slow path to avoid having two emit-based delegates. |
| 61 | + if (_emitInvoke != null && obj == null) |
| 62 | + { |
| 63 | + return _emitInvoke(this, obj, args); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + return _method.InvokeNonEmitUnsafe(obj, args, argsForTemporaryMonoSupport, invokeAttr); |
| 68 | + } |
| 69 | + } |
| 70 | + catch (Exception e) |
| 71 | + { |
| 72 | + throw new TargetInvocationException(e); |
| 73 | + } |
| 74 | + } |
| 75 | + else |
| 76 | + { |
| 77 | + if (_emitInvoke != null && obj == null) |
| 78 | + { |
| 79 | + return _emitInvoke(this, obj, args); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + return _method.InvokeNonEmitUnsafe(obj, args, argsForTemporaryMonoSupport, invokeAttr); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + // Remove this branch once Mono has the same exception handling and managed conversion logic. |
| 90 | + return _method.InvokeNonEmitUnsafe(obj, args, argsForTemporaryMonoSupport, invokeAttr); |
| 91 | + } |
24 | 92 | }
|
25 | 93 | }
|
26 | 94 | }
|
0 commit comments