12
12
13
13
namespace System . Reflection
14
14
{
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"/>
15
26
public sealed partial class MethodInvoker
16
27
{
17
28
private InvokeFunc_ObjSpanArgs ? _invokeFunc_ObjSpanArgs ;
@@ -26,6 +37,17 @@ public sealed partial class MethodInvoker
26
37
private readonly bool _needsByRefStrategy ;
27
38
private readonly bool _isStatic ;
28
39
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>
29
51
public static MethodInvoker Create ( MethodBase method )
30
52
{
31
53
ArgumentNullException . ThrowIfNull ( method , nameof ( method ) ) ;
@@ -60,6 +82,32 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
60
82
Initialize ( argumentTypes , out _strategy , out _invokerArgFlags , out _needsByRefStrategy ) ;
61
83
}
62
84
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>
63
111
public object ? Invoke ( object ? obj )
64
112
{
65
113
if ( _argCount != 0 )
@@ -70,6 +118,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
70
118
return InvokeImpl ( obj , null , null , null , null ) ;
71
119
}
72
120
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>
73
127
public object ? Invoke ( object ? obj , object ? arg1 )
74
128
{
75
129
if ( _argCount != 1 )
@@ -80,6 +134,10 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
80
134
return InvokeImpl ( obj , arg1 , null , null , null ) ;
81
135
}
82
136
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>
83
141
public object ? Invoke ( object ? obj , object ? arg1 , object ? arg2 )
84
142
{
85
143
if ( _argCount != 2 )
@@ -90,6 +148,11 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
90
148
return InvokeImpl ( obj , arg1 , arg2 , null , null ) ;
91
149
}
92
150
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>
93
156
public object ? Invoke ( object ? obj , object ? arg1 , object ? arg2 , object ? arg3 )
94
157
{
95
158
if ( _argCount != 3 )
@@ -100,6 +163,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
100
163
return InvokeImpl ( obj , arg1 , arg2 , arg3 , null ) ;
101
164
}
102
165
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>
103
172
public object ? Invoke ( object ? obj , object ? arg1 , object ? arg2 , object ? arg3 , object ? arg4 )
104
173
{
105
174
if ( _argCount != 4 )
@@ -156,6 +225,12 @@ private MethodInvoker(MethodBase method, RuntimeType[] argumentTypes)
156
225
return InvokeDirectByRef ( obj , arg1 , arg2 , arg3 , arg4 ) ;
157
226
}
158
227
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>
159
234
public object ? Invoke ( object ? obj , Span < object ? > arguments )
160
235
{
161
236
int argLen = arguments . Length ;
0 commit comments