Export (0) Print
Expand All

ParameterInfo.Position Property

Gets the zero-based position of the parameter in the formal parameter list.

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

public virtual int Position { get; }

Property Value

Type: System.Int32
The position this parameter occupies in the parameter list.

Only parameters in the method signature (that is, in the formal parameter list) are considered when calculating the position. For instance methods, the hidden parameter that represents this (Me in Visual Basic) is not counted.

To get the ParameterInfo array, first get the method or the constructor, and then call MethodBase.GetParameters.

The following example defines a method with four parameters and displays various properties, including the Position property to display the positions 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)
 */


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft