MethodBase.GetParameters 方法

定义

当在派生类中重写时,获取指定的方法或构造函数的参数。When overridden in a derived class, gets the parameters of the specified method or constructor.

public:
 abstract cli::array <System::Reflection::ParameterInfo ^> ^ GetParameters();
public abstract System.Reflection.ParameterInfo[] GetParameters ();
abstract member GetParameters : unit -> System.Reflection.ParameterInfo[]
Public MustOverride Function GetParameters () As ParameterInfo()

返回

ParameterInfo 类型的数组,包含与此 MethodBase 实例所反射的方法(或构造函数)的签名匹配的信息。An array of type ParameterInfo containing information that matches the signature of the method (or constructor) reflected by this MethodBase instance.

实现

示例

下面的示例使用 GetParameters 方法检索委托的 Invoke 方法的参数。The following example uses the GetParameters method to retrieve the parameters of the Invoke method of a delegate.

该示例定义一个名为 MyDelegate 的委托和一个 MyDelegate类型的名为 ev 的事件。The example defines a delegate named MyDelegate and an event named ev of type MyDelegate. Main 方法中的代码通过获取事件的委托类型,获取委托类型的 Invoke 方法,然后检索并显示参数来发现事件签名。The code in the Main method discovers the event signature by getting the delegate type of the event, getting the Invoke method of the delegate type, and then retrieving and displaying the parameters.

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

public delegate void MyDelegate( int i );
public ref class MainClass
{
public:
   event MyDelegate^ ev;
};

int main()
{
   Type^ delegateType = MainClass::typeid->GetEvent( "ev" )->EventHandlerType;
   MethodInfo^ invoke = delegateType->GetMethod( "Invoke" );
   array<ParameterInfo^>^pars = invoke->GetParameters();
   System::Collections::IEnumerator^ myEnum = pars->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      ParameterInfo^ p = safe_cast<ParameterInfo^>(myEnum->Current);
      Console::WriteLine( p->ParameterType );
   }
}
// The example displays the following output:
//       System.Int32
// 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
Imports System.Reflection

Public Delegate Sub MyDelegate(ByVal i As Integer)

Public Class MainClass
    Public Event ev As MyDelegate

    Public Shared Sub Main()
        Dim delegateType As Type = GetType(MainClass).GetEvent("ev").EventHandlerType
        Dim invoke As MethodInfo = delegateType.GetMethod("Invoke")
        Dim pars As ParameterInfo() = invoke.GetParameters()
        For Each p As ParameterInfo In pars
            Console.WriteLine(p.ParameterType)
        Next 
    End Sub 
End Class 
' The example displays the following output:
'     System.Int32

适用于

另请参阅