ParameterInfo.ParameterType 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取该参数的 Type
。
public:
virtual property Type ^ ParameterType { Type ^ get(); };
public virtual Type ParameterType { get; }
member this.ParameterType : Type
Public Overridable ReadOnly Property ParameterType As Type
属性值
表示该参数 Type
的 Type
对象。
示例
以下示例演示如何获取 ParameterInfo 方法参数的对象,然后使用 ParameterType 属性显示每个参数的类型。
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
public ref class parminfo
{
public:
static void mymethod( int int1m, [Out]interior_ptr<String^> str2m, interior_ptr<String^> str3m )
{
*str2m = "in mymethod";
}
};
int main()
{
Console::WriteLine( "\nReflection.Parameterinfo" );
//Get the ParameterInfo parameter of a function.
//Get the type.
Type^ Mytype = Type::GetType( "parminfo" );
//Get and display the method.
MethodBase^ Mymethodbase = Mytype->GetMethod( "mymethod" );
Console::Write( "\nMymethodbase = {0}", Mymethodbase );
//Get the ParameterInfo array.
array<ParameterInfo^>^Myarray = Mymethodbase->GetParameters();
//Get and display the ParameterInfo of each parameter.
System::Collections::IEnumerator^ enum0 = Myarray->GetEnumerator();
while ( enum0->MoveNext() )
{
ParameterInfo^ Myparam = safe_cast<ParameterInfo^>(enum0->Current);
Console::Write( "\nFor parameter # {0}, the ParameterType is - {1}", Myparam->Position, Myparam->ParameterType );
}
return 0;
}
/*
This code produces the following output:
Reflection.Parameterinfo
Mymethodbase = Void mymethod(Int32, System.String ByRef, System.String ByRef)
For parameter # 0, the ParameterType is - System.Int32
For parameter # 1, the ParameterType is - System.String&
For parameter # 2, the ParameterType is - System.String&
*/
using System;
using System.Reflection;
class parminfo
{
public static void mymethod (
int int1m, out string str2m, ref string str3m)
{
str2m = "in mymethod";
}
public static int Main(string[] args)
{
Console.WriteLine("\nReflection.Parameterinfo");
//Get the ParameterInfo parameter of a function.
//Get the type.
Type Mytype = Type.GetType("parminfo");
//Get and display the method.
MethodBase Mymethodbase = Mytype.GetMethod("mymethod");
Console.Write("\nMymethodbase = " + Mymethodbase);
//Get the ParameterInfo array.
ParameterInfo[]Myarray = Mymethodbase.GetParameters();
//Get and display the ParameterInfo of each parameter.
foreach (ParameterInfo Myparam in Myarray)
{
Console.Write ("\nFor parameter # " + Myparam.Position
+ ", the ParameterType is - " + Myparam.ParameterType);
}
return 0;
}
}
/*
This code produces the following output:
Reflection.Parameterinfo
Mymethodbase = Void mymethod(int, System.String ByRef, System.String ByRef)
For parameter # 0, the ParameterType is - System.Int32
For parameter # 1, the ParameterType is - System.String&
For parameter # 2, the ParameterType is - System.String&
*/
Imports System.Reflection
Class parminfo
Public Shared Sub mymethod(int1m As Integer, ByRef str2m As String, _
ByRef str3m As String)
str2m = "in mymethod"
End Sub
Public Shared Function Main() As Integer
Console.WriteLine(ControlChars.CrLf + "Reflection.Parameterinfo")
'Get the ParameterInfo parameter of a function.
'Get the type.
Dim Mytype As Type = GetType(parminfo)
'Get and display the method.
Dim Mymethodbase As MethodBase = Mytype.GetMethod("mymethod")
Console.Write(ControlChars.CrLf _
+ "Mymethodbase = " + Mymethodbase.ToString())
'Get the ParameterInfo array.
Dim Myarray As ParameterInfo() = Mymethodbase.GetParameters()
'Get and display the ParameterInfo of each parameter.
Dim Myparam As ParameterInfo
For Each Myparam In Myarray
Console.Write(ControlChars.CrLf _
+ "For parameter # " + Myparam.Position.ToString() _
+ ", the ParameterType is - " + Myparam.ParameterType.ToString())
Next Myparam
Return 0
End Function
End Class
' This code produces the following output:
'
' Reflection.Parameterinfo
' Mymethodbase = Void mymethod(Int32, System.String ByRef, System.String ByRef)
' For parameter # 0, the ParameterType is - System.Int32
' For parameter # 1, the ParameterType is - System.String&
' For parameter # 2, the ParameterType is - System.String&
注解
此方法依赖于可选的元数据,可能并非在所有编译器中都可用。
若要获取 ParameterInfo 数组,请先获取 方法或构造函数,然后调用 MethodBase.GetParameters。