MethodBase.GetParameters 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当在派生类中重写时,获取指定的方法或构造函数的参数。
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
实例所反射的方法(或构造函数)的签名匹配的信息。
实现
示例
以下示例使用 GetParameters 方法检索委托方法 Invoke
的参数。
该示例定义名为 的MyDelegate
委托和类型为 的MyDelegate
名为 ev
的事件。 方法中的 Main
代码通过获取事件的委托类型、获取 Invoke
委托类型的 方法,然后检索和显示参数来发现事件签名。
// 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
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将推出:在整个 2024 年,我们将逐步取消以“GitHub 问题”作为内容的反馈机制,并将其替换为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈