1212
1313namespace 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