Skip to content

Commit 26b1ce7

Browse files
authored
Add doc for the new invoker classes (#90536)
1 parent 782aea1 commit 26b1ce7

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111

1212
namespace System.Reflection
1313
{
14+
/// <summary>
15+
/// Invokes the method reflected by the provided <see cref="ConstructorInfo"/>.
16+
/// </summary>
17+
/// <remarks>
18+
/// Used for better performance than <seealso cref="ConstructorInfo.Invoke"/> when compatibility with that method
19+
/// is not necessary and when the caller can cache the ConstructorInvoker instance for additional invoke calls.<br/>
20+
/// Unlike <see cref="ConstructorInfo.Invoke"/>, the invoke methods do not look up default values for arguments when
21+
/// <see cref="Type.Missing"/> is specified. In addition, the target constructor may be inlined for performance and not
22+
/// appear in stack traces.
23+
/// </remarks>
24+
/// <seealso cref="MethodInvoker"/>
1425
public sealed partial class ConstructorInvoker
1526
{
1627
private InvokeFunc_ObjSpanArgs? _invokeFunc_ObjSpanArgs;
@@ -24,6 +35,17 @@ public sealed partial class ConstructorInvoker
2435
private readonly RuntimeConstructorInfo _method;
2536
private readonly bool _needsByRefStrategy;
2637

38+
/// <summary>
39+
/// Creates a new instance of ConstructorInvoker.
40+
/// </summary>
41+
/// <remarks>
42+
/// For performance, the resulting instance should be cached for additional calls.
43+
/// </remarks>
44+
/// <param name="constructor">The constructor that will be invoked.</param>
45+
/// <returns>An instance of a ConstructorInvoker.</returns>
46+
/// <exception cref="ArgumentException">
47+
/// The <paramref name="constructor"/> is not a runtime-based method.
48+
/// </exception>
2749
public static ConstructorInvoker Create(ConstructorInfo constructor)
2850
{
2951
ArgumentNullException.ThrowIfNull(constructor, nameof(constructor));
@@ -46,6 +68,21 @@ private ConstructorInvoker(RuntimeConstructorInfo constructor, RuntimeType[] arg
4668
Initialize(argumentTypes, out _strategy, out _invokerArgFlags, out _needsByRefStrategy);
4769
}
4870

71+
/// <summary>
72+
/// Invokes the constructor.
73+
/// </summary>
74+
/// <returns>
75+
/// An instance of the class associated with the constructor.
76+
/// </returns>
77+
/// <exception cref="InvalidOperationException">
78+
/// The type that declares the method is an open generic type.
79+
/// </exception>
80+
/// <exception cref="TargetParameterCountException">
81+
/// The correct number of arguments were not provided.
82+
/// </exception>
83+
/// <exception cref="NotSupportedException">
84+
/// The calling convention or signature is not supported.
85+
/// </exception>
4986
public object Invoke()
5087
{
5188
if (_argCount != 0)
@@ -56,6 +93,14 @@ public object Invoke()
5693
return InvokeImpl(null, null, null, null);
5794
}
5895

96+
/// <summary>
97+
/// Invokes the constructor using the specified parameters.
98+
/// </summary>
99+
/// <inheritdoc cref="Invoke()"/>
100+
/// <param name="arg1">The first argument for the invoked method.</param>
101+
/// <exception cref="ArgumentException">
102+
/// The arguments do not match the signature of the invoked constructor.
103+
/// </exception>
59104
public object Invoke(object? arg1)
60105
{
61106
if (_argCount != 1)
@@ -66,6 +111,9 @@ public object Invoke(object? arg1)
66111
return InvokeImpl(arg1, null, null, null);
67112
}
68113

114+
/// <inheritdoc cref="Invoke(object?)"/>
115+
/// <param name="arg1">The first argument for the invoked method.</param>
116+
/// <param name="arg2">The second argument for the invoked method.</param>
69117
public object Invoke(object? arg1, object? arg2)
70118
{
71119
if (_argCount != 2)
@@ -76,6 +124,10 @@ public object Invoke(object? arg1, object? arg2)
76124
return InvokeImpl(arg1, arg2, null, null);
77125
}
78126

127+
/// <inheritdoc cref="Invoke(object?)"/>
128+
/// <param name="arg1">The first argument for the invoked method.</param>
129+
/// <param name="arg2">The second argument for the invoked method.</param>
130+
/// <param name="arg3">The third argument for the invoked method.</param>
79131
public object Invoke(object? arg1, object? arg2, object? arg3)
80132
{
81133
if (_argCount !=3)
@@ -86,6 +138,11 @@ public object Invoke(object? arg1, object? arg2, object? arg3)
86138
return InvokeImpl(arg1, arg2, arg3, null);
87139
}
88140

141+
/// <inheritdoc cref="Invoke(object?)"/>
142+
/// <param name="arg1">The first argument for the invoked method.</param>
143+
/// <param name="arg2">The second argument for the invoked method.</param>
144+
/// <param name="arg3">The third argument for the invoked method.</param>
145+
/// <param name="arg4">The fourth argument for the invoked method.</param>
89146
public object Invoke(object? arg1, object? arg2, object? arg3, object? arg4)
90147
{
91148
if (_argCount != 4)
@@ -137,6 +194,11 @@ private object InvokeImpl(object? arg1, object? arg2, object? arg3, object? arg4
137194
return InvokeDirectByRef(arg1, arg2, arg3, arg4);
138195
}
139196

197+
/// <inheritdoc cref="Invoke(object?)"/>
198+
/// <param name="arguments">The arguments for the invoked constructor.</param>
199+
/// <exception cref="ArgumentException">
200+
/// The arguments do not match the signature of the invoked constructor.
201+
/// </exception>
140202
public object Invoke(Span<object?> arguments)
141203
{
142204
int argLen = arguments.Length;

src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvoker.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@
1212

1313
namespace System.Reflection
1414
{
15+
/// <summary>
16+
/// Invokes the method reflected by the provided <see cref="MethodBase"/>.
17+
/// </summary>
18+
/// <remarks>
19+
/// Used for better performance than <seealso cref="MethodBase.Invoke"/> when compatibility with that method
20+
/// is not necessary and when the caller can cache the MethodInvoker instance for additional invoke calls.<br/>
21+
/// Unlike <see cref="MethodBase.Invoke"/>, the invoke methods do not look up default values for arguments when
22+
/// <see cref="Type.Missing"/> is specified. In addition, the target method may be inlined for performance and not
23+
/// appear in stack traces.
24+
/// </remarks>
25+
/// <seealso cref="ConstructorInvoker"/>
1526
public sealed partial class MethodInvoker
1627
{
1728
private InvokeFunc_ObjSpanArgs? _invokeFunc_ObjSpanArgs;
@@ -26,6 +37,17 @@ public sealed partial class MethodInvoker
2637
private readonly bool _needsByRefStrategy;
2738
private readonly bool _isStatic;
2839

40+
/// <summary>
41+
/// Creates a new instance of MethodInvoker.
42+
/// </summary>
43+
/// <remarks>
44+
/// For performance, the resulting instance should be cached for additional calls.
45+
/// </remarks>
46+
/// <param name="method">The method that will be invoked.</param>
47+
/// <returns>An instance of a MethodInvoker.</returns>
48+
/// <exception cref="ArgumentException">
49+
/// The <paramref name="method"/> is not a runtime-based method.
50+
/// </exception>
2951
public static MethodInvoker Create(MethodBase method)
3052
{
3153
ArgumentNullException.ThrowIfNull(method, nameof(method));
@@ -60,6 +82,32 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
6082
Initialize(argumentTypes, out _strategy, out _invokerArgFlags, out _needsByRefStrategy);
6183
}
6284

85+
/// <summary>
86+
/// Invokes the method using the specified parameters.
87+
/// </summary>
88+
/// <param name="obj">
89+
/// The object on which to invoke the method. If the method is static, this argument is ignored.
90+
/// </param>
91+
/// <returns>
92+
/// An object containing the return value of the invoked method,
93+
/// or <c>null</c> if the invoked method does not have a return value.
94+
/// </returns>
95+
/// <exception cref="TargetException">
96+
/// The <para>obj</para> parameter is <c>null</c> and the method is not static.
97+
///
98+
/// -or-
99+
///
100+
/// The method is not declared or inherited by the class of <para>obj</para>.
101+
/// </exception>
102+
/// <exception cref="InvalidOperationException">
103+
/// The type that declares the method is an open generic type.
104+
/// </exception>
105+
/// <exception cref="TargetParameterCountException">
106+
/// The correct number of arguments were not provided.
107+
/// </exception>
108+
/// <exception cref="NotSupportedException">
109+
/// The calling convention or signature is not supported.
110+
/// </exception>
63111
public object? Invoke(object? obj)
64112
{
65113
if (_argCount != 0)
@@ -70,6 +118,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
70118
return InvokeImpl(obj, null, null, null, null);
71119
}
72120

121+
/// <inheritdoc cref="Invoke(object?)"/>
122+
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
123+
/// <param name="arg1">The first argument for the invoked method.</param>
124+
/// <exception cref="ArgumentException">
125+
/// The arguments do not match the signature of the invoked method.
126+
/// </exception>
73127
public object? Invoke(object? obj, object? arg1)
74128
{
75129
if (_argCount != 1)
@@ -80,6 +134,10 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
80134
return InvokeImpl(obj, arg1, null, null, null);
81135
}
82136

137+
/// <inheritdoc cref="Invoke(object?)"/>
138+
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
139+
/// <param name="arg1">The first argument for the invoked method.</param>
140+
/// <param name="arg2">The second argument for the invoked method.</param>
83141
public object? Invoke(object? obj, object? arg1, object? arg2)
84142
{
85143
if (_argCount != 2)
@@ -90,6 +148,11 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
90148
return InvokeImpl(obj, arg1, arg2, null, null);
91149
}
92150

151+
/// <inheritdoc cref="Invoke(object?)"/>
152+
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
153+
/// <param name="arg1">The first argument for the invoked method.</param>
154+
/// <param name="arg2">The second argument for the invoked method.</param>
155+
/// <param name="arg3">The third argument for the invoked method.</param>
93156
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3)
94157
{
95158
if (_argCount != 3)
@@ -100,6 +163,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
100163
return InvokeImpl(obj, arg1, arg2, arg3, null);
101164
}
102165

166+
/// <inheritdoc cref="Invoke(object?)"/>
167+
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
168+
/// <param name="arg1">The first argument for the invoked method.</param>
169+
/// <param name="arg2">The second argument for the invoked method.</param>
170+
/// <param name="arg3">The third argument for the invoked method.</param>
171+
/// <param name="arg4">The fourth argument for the invoked method.</param>
103172
public object? Invoke(object? obj, object? arg1, object? arg2, object? arg3, object? arg4)
104173
{
105174
if (_argCount != 4)
@@ -156,6 +225,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
156225
return InvokeDirectByRef(obj, arg1, arg2, arg3, arg4);
157226
}
158227

228+
/// <inheritdoc cref="Invoke(object?)"/>
229+
/// <param name="obj"> The object on which to invoke the method. If the method is static, this argument is ignored. </param>
230+
/// <param name="arguments">The arguments for the invoked method.</param>
231+
/// <exception cref="ArgumentException">
232+
/// The arguments do not match the signature of the invoked method.
233+
/// </exception>
159234
public object? Invoke(object? obj, Span<object?> arguments)
160235
{
161236
int argLen = arguments.Length;

0 commit comments

Comments
 (0)