Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Internal.Reflection.Core.Execution;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Runtime.MethodInfos;
using static System.Reflection.DynamicInvokeInfo;

Expand All @@ -11,11 +12,13 @@ namespace System.Reflection
public sealed class ConstructorInvoker
{
private readonly MethodBaseInvoker _methodBaseInvoker;
private readonly int _parameterCount;
private readonly RuntimeTypeHandle _declaringTypeHandle;

internal ConstructorInvoker(RuntimeConstructorInfo constructor)
{
_methodBaseInvoker = constructor.MethodInvoker;
_parameterCount = constructor.GetParametersNoCopy().Length;
_declaringTypeHandle = constructor.DeclaringType.TypeHandle;
}

Expand All @@ -40,43 +43,67 @@ public object Invoke()
[DebuggerGuidedStepThrough]
public object Invoke(object? arg1)
{
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1));
if (_parameterCount > 4)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

object result = _methodBaseInvoker.CreateInstanceWithFewArgs(new Span<object?>(ref arg1, _parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object Invoke(object? arg1, object? arg2)
{
if (_parameterCount > 2)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(2));
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object Invoke(object? arg1, object? arg2, object? arg3)
{
if (_parameterCount > 3)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(3));
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object Invoke(object? arg1, object? arg2, object? arg3, object? arg4)
{
if (_parameterCount > 4)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
argStorage._args.Set(3, arg4);
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(4));
object result = _methodBaseInvoker.CreateInstanceWithFewArgs(argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -88,5 +115,11 @@ public object Invoke(Span<object?> arguments)
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DoesNotReturn]
private static void ThrowForArgCountMismatch()
{
throw new TargetParameterCountException(SR.Arg_ParmCnt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Internal.Reflection.Core.Execution;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection.Runtime.MethodInfos;
using static System.Reflection.DynamicInvokeInfo;

Expand All @@ -11,10 +12,12 @@ namespace System.Reflection
public sealed class MethodInvoker
{
private readonly MethodBaseInvoker _methodBaseInvoker;
private readonly int _parameterCount;

internal MethodInvoker(RuntimeMethodInfo method)
{
_methodBaseInvoker = method.MethodInvoker;
_parameterCount = method.GetParametersNoCopy().Length;
}

internal MethodInvoker(RuntimeConstructorInfo constructor)
Expand Down Expand Up @@ -52,46 +55,70 @@ public static MethodInvoker Create(MethodBase method)
[DebuggerGuidedStepThrough]
public object? Invoke(object? obj, object? arg1)
{
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, new Span<object?>(ref arg1));
if (_parameterCount > 1)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, new Span<object?>(ref arg1, _parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object? Invoke(object? obj, object? arg1, object? arg2)
{
if (_parameterCount > 2)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(2));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3)
{
if (_parameterCount > 3)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(3));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DebuggerGuidedStepThrough]
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3, object? arg4)
{
if (_parameterCount > 4)
{
// Ignore extra parameters, but throw if not enough to avoid referencing invalid memory.
ThrowForArgCountMismatch();
}

StackAllocatedArguments argStorage = default;
argStorage._args.Set(0, arg1);
argStorage._args.Set(1, arg2);
argStorage._args.Set(2, arg3);
argStorage._args.Set(3, arg4);

object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(4));
object? result = _methodBaseInvoker.InvokeDirectWithFewArgs(obj, argStorage._args.AsSpan(_parameterCount));
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}
Expand All @@ -103,5 +130,11 @@ public static MethodInvoker Create(MethodBase method)
DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
return result;
}

[DoesNotReturn]
private static void ThrowForArgCountMismatch()
{
throw new TargetParameterCountException(SR.Arg_ParmCnt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.Serialization;

namespace Microsoft.Extensions.Internal
{
Expand Down
Loading