ParameterInfo.Name Свойство
В этой статье
Определение
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает имя параметра.
public:
virtual property System::String ^ Name { System::String ^ get(); };
public virtual string Name { get; }
public virtual string? Name { get; }
member this.Name : string
Public Overridable ReadOnly Property Name As String
Простое имя параметра.
В следующем примере показано, как получить ParameterInfo объекты для параметров метода, а затем использовать Name свойство для получения имен параметров.
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 name of each parameter.
foreach (ParameterInfo Myparam in Myarray)
{
Console.Write ("\nFor parameter # " + Myparam.Position
+ ", the Name is - " + Myparam.Name);
}
return 0;
}
}
/*
This code produces the following output:
Reflection.ParameterInfo
Mymethodbase
= Void mymethod (int, System.String ByRef, System.String ByRef)
For parameter # 0, the Name is - int1m
For parameter # 1, the Name is - str2m
For parameter # 2, the Name is - str3m
*/
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 = 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 name of each parameter.
Dim Myparam As ParameterInfo
For Each Myparam In Myarray
Console.Write(ControlChars.CrLf _
+ "For parameter # " + Myparam.Position.ToString() _
+ ", the Name is - " + Myparam.Name)
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 Name is - int1m
' For parameter # 1, the Name is - str2m
' For parameter # 2, the Name is - str3m
Это свойство использует защищенное NameImpl поле и зависит от необязательного флага метаданных, который может быть доступен не во всех компиляторах.
Чтобы получить ParameterInfo массив, сначала получите метод или конструктор, а затем вызовите MethodBase.GetParameters метод .
Предупреждение
Если объект ParameterInfo представляет возвращаемое значение (то есть, если оно было получено с помощью MethodInfo.ReturnParameter свойства ), это свойство будет иметь значение null
.
Применяется к
Продукт | Версии |
---|---|
.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, 10 |
.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 |