MethodBase.GetParameters 方法

定義

在衍生類別中覆寫時,取得指定方法或建構函式的參數。

C#
public abstract System.Reflection.ParameterInfo[] GetParameters ();

傳回

ParameterInfo 型別的陣列,含有與這個 MethodBase 執行個體反映的方法 (或建構函式) 簽章相符的資訊。

實作

範例

下列範例會 GetParameters 使用 方法來擷取 Invoke 委派方法的參數。

此範例會定義名為 MyDelegate 的委派,以及名為 類型的MyDelegate事件ev。 方法中的 Main 程式代碼會藉由取得事件的委派類型、取得 Invoke 委派類型的方法,然後擷取和顯示參數,來探索事件簽章。

C#
// The following example uses instances of classes in
// the System.Reflection namespace to discover an event argument type.
using System;
using System.Reflection;

public delegate void MyDelegate(int i);
public class MainClass
{
    public event MyDelegate ev;

    public static void Main()
    {
        Type delegateType = typeof(MainClass).GetEvent("ev").EventHandlerType;
        MethodInfo invoke = delegateType.GetMethod("Invoke");
        ParameterInfo[] pars = invoke.GetParameters();
        foreach (ParameterInfo p in pars)
        {
            Console.WriteLine(p.ParameterType);
        }
    }
}
// The example displays the following output:
//       System.Int32

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另請參閱