Прочитать на английском

Поделиться через


ParameterInfo.Attributes Свойство

Определение

Получает атрибуты этого параметра.

public virtual System.Reflection.ParameterAttributes Attributes { get; }

Значение свойства

Объект ParameterAttributes, который представляет атрибуты этого параметра.

Примеры

В следующем примере определяется метод с тремя параметрами. Оно использует свойство , Attributes чтобы получить атрибуты второго параметра и отобразить их в консоли.

using System;
using System.Reflection;
public class MyClass1
{
   public int MyMethod( int i, out short j, ref long k)
   {
      j = 2;
      return 0;
   }
}

public class ParameterInfo_Attributes
{
   public static void Main()
   {
      // Get the type.
      Type myType = typeof(MyClass1);
      // Get the method named 'MyMethod' from the type.
      MethodBase myMethodBase = myType.GetMethod("MyMethod");
      // Get the parameters associated with the method.
      ParameterInfo[] myParameters = myMethodBase.GetParameters();
      Console.WriteLine("\nThe method {0} has the {1} parameters :",
                          "ParameterInfo_Example.MyMethod", myParameters.Length);
      // Print the attributes associated with each of the parameters.
      for(int i = 0; i < myParameters.Length; i++)
         Console.WriteLine("\tThe {0} parameter has the attribute : {1}",
                                             i + 1, myParameters[i].Attributes);
   }
}

Комментарии

Этот метод использует AttrsImpl метод .

Чтобы получить ParameterInfo массив, сначала получите метод или конструктор, а затем вызовите MethodBase.GetParameters.

Применяется к

Продукт Версии
.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

См. также раздел