ParameterInfo Class

July 28, 2014

Discovers the attributes of a parameter and provides access to parameter metadata.

System.Object
  System.Reflection.ParameterInfo

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

public class ParameterInfo : ICustomAttributeProvider

The ParameterInfo type exposes the following members.

  NameDescription
Protected methodParameterInfoInitializes a new instance of the ParameterInfo class.
Top

  NameDescription
Public propertyAttributesGets the attributes for this parameter.
Public propertyCustomAttributesGets a collection that contains this parameter's custom attributes.
Public propertyDefaultValueGets the default value, if the parameter has a default value.
Public propertyHasDefaultValueGets a value that indicates whether this parameter has a default value.
Public propertyIsInGets a value indicating whether this is an input parameter.
Public propertyIsOptionalGets a value indicating whether this parameter is optional.
Public propertyIsOutGets a value indicating whether this is an output parameter.
Public propertyIsRetvalGets a value indicating whether this is a Retval parameter.
Public propertyMemberGets the member in which the parameter is defined.
Public propertyMetadataTokenGets a value that identifies this parameter in metadata.
Public propertyNameGets the name of the parameter.
Public propertyParameterTypeGets the type of this parameter.
Public propertyPositionGets the zero-based position of the parameter in the formal parameter list.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCustomAttributes(Boolean)Gets all the custom attributes defined on this parameter.
Public methodGetCustomAttributes(Type, Boolean)Gets the custom attributes of the specified type or its derived types that are applied to this parameter.
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefinedDetermines whether the custom attribute of the specified type or its derived types is applied to this parameter.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringGets the parameter type and name represented as a string. (Overrides Object.ToString().)
Top

  NameDescription
Public Extension MethodGetCustomAttribute(Type)Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute(Type, Boolean)Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute<T>()Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttribute<T>(Boolean)Overloaded. Retrieves a custom attribute of a specified type that is applied to a specified parameter, and optionally inspects the ancestors of that parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes()Overloaded. Retrieves a collection of custom attributes that are applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes(Type)Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes<T>()Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodGetCustomAttributes<T>(Boolean)Overloaded. Retrieves a collection of custom attributes of a specified type that are applied to a specified parameter, and optionally inspects the ancestors of that parameter. (Defined by CustomAttributeExtensions.)
Public Extension MethodIsDefinedIndicates whether custom attributes of a specified type are applied to a specified parameter. (Defined by CustomAttributeExtensions.)
Top

Use an instance of ParameterInfo to obtain information about the parameter's data type, default value, and so on.

MethodBase.GetParameters returns an array of ParameterInfo objects representing the parameters of a method, in order.

The following example defines a method with four parameters and uses the ParameterInfo.Attributes property to display the attributes of the parameters.


using System;
using System.Reflection;
using System.Runtime.InteropServices;

class Example
{
   public static void mymethod(string str1, ref string str2, out string str3,
      [In] string str4)
   {
      // Concatenate str1 to str2, which is ref.
      str2 += str1;
      // When mymethod is called, str3 has no value. Give it one.
      str3 = "new value";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      MethodInfo mm = typeof(Example).GetMethod("mymethod");

      // Display the method.
      outputBlock.Text += "MethodInfo.ToString(): " + mm.ToString() + "\n";

      // Get and display the attributes for the second parameter.
      foreach (ParameterInfo param in mm.GetParameters())
      {
         outputBlock.Text += String.Format("Attributes for parameter {0}, \"{1}\": {2} ({3})",  
            param.Position, param.Name, param.Attributes, (int)param.Attributes);

         if (param.ParameterType.IsByRef)
         {
            outputBlock.Text += "; the parameter type is ref\n";
         }
         else
         {
            outputBlock.Text += "\n";
         }
      }
   }
}

/* This code produces the following output:

MethodInfo.ToString(): Void mymethod(System.String, System.String ByRef, System.String ByRef)
Attributes for parameter 0, "str1": None (0)
Attributes for parameter 1, "str2": None (0); the parameter type is ByRef
Attributes for parameter 2, "str3": Out (2); the parameter type is ByRef
Attributes for parameter 3, "str4": In (1)
 */


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

This type is thread safe.

Show:
© 2014 Microsoft