System.Reflection.ParameterInfo Class

Assembly: Mscorlib.dll
Namespace: System.Reflection
Summary
Discovers the attributes of a parameter and provides access to parameter metadata.
C# Syntax:
[Serializable]
public class ParameterInfo : ICustomAttributeProvider
Thread Safety
This type is safe for multithreaded operations.
Remarks
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.

See also:
System.Reflection Namespace

System.Reflection.ParameterInfo Member List:

Public Properties
Attributes Read-only

Gets the attributes for this parameter.
DefaultValue Read-only

Gets a value indicating the default value of the parameter has a default value.
IsIn Read-only

Gets a value indicating whether this is an input parameter.
IsLcid Read-only

Gets a value indicating whether this parameter is a locale identifier (lcid).
IsOptional Read-only

Gets a value indicating whether this parameter is optional.
IsOut Read-only

Gets a value indicating whether this is an output parameter.
IsRetval Read-only

Gets a value indicating whether this is a Retval parameter.
Member Read-only

Gets a value indicating the member in which the parameter is implemented.
Name Read-only

Gets the name of the parameter.
ParameterType Read-only

Gets the Type of this parameter.
Position Read-only

Gets the signature position for the parameter.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetCustomAttributes Overloaded:
GetCustomAttributes(bool inherit)

Gets all the custom attributes defined on this parameter.
GetCustomAttributes Overloaded:
GetCustomAttributes(Type attributeType, bool inherit)

Gets the custom attributes of the specified type defined on this parameter.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
IsDefined Determines if the custom attribute of the specified type is defined on this member.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the ParameterInfo class.
Protected Fields
AttrsImpl The attributes of the parameter.
ClassImpl The Type of the parameter.
DefaultValueImpl The default value of the parameter.
MemberImpl The member in which the field is implemented.
NameImpl The name of the parameter.
PositionImpl The zero-based position of the parameter in the parameter list.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Reflection.ParameterInfo Member Details

ctor #1
Summary
Initializes a new instance of the ParameterInfo class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
protected ParameterInfo();

Return to top


Field: AttrsImpl
Summary
The attributes of the parameter.
C# Syntax:
protected ParameterAttributes AttrsImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to parameter attributes is through ParameterInfo.Attributes.

Return to top


Field: ClassImpl
Summary
The Type of the parameter.
C# Syntax:
protected Type ClassImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to parameter types is through ParameterInfo.ParameterType.

Return to top


Field: DefaultValueImpl
Summary
The default value of the parameter.
C# Syntax:
protected object DefaultValueImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to the default value of the parameter is through ParameterInfo.DefaultValue.

Return to top


Field: MemberImpl
Summary
The member in which the field is implemented.
C# Syntax:
protected MemberInfo MemberImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to the parameter name is through the ParameterInfo.Member.

Return to top


Field: NameImpl
Summary
The name of the parameter.
C# Syntax:
protected string NameImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to the parameter name is through the ParameterInfo.Name.

Return to top


Field: PositionImpl
Summary
The zero-based position of the parameter in the parameter list.
C# Syntax:
protected int PositionImpl;
Remarks
This field is intended only for users who are deriving classes from ParameterInfo.

Typical access to the name of the parameter is through ParameterInfo.Position.

Return to top


Property: Attributes (read-only)
Summary
Gets the attributes for this parameter.
C# Syntax:
public virtual ParameterAttributes Attributes {get;}
Remarks
This method utilizes the ParameterInfo.AttrsImpl method.

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

Example
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);
   }
}

    
See also:
ParameterAttributes

Return to top


Property: DefaultValue (read-only)
Summary
Gets a value indicating the default value of the parameter has a default value.
C# Syntax:
public virtual object DefaultValue {get;}
Remarks
The default value is used when an actual value is not specified in the method call. A parameter can have a default value that is null. This is distinct from the case where a default value is not defined.

This method utilizes the ParameterInfo.DefaultValueImpl method.

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

Return to top


Property: IsIn (read-only)
Summary
Gets a value indicating whether this is an input parameter.
C# Syntax:
public bool IsIn {get;}
Remarks
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the In flag of the ParameterAttributes enumerator.

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

Example

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class ParameterInfo_IsIn_IsOut_IsOptional
{
   public static void DefineMethod()
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "MyAssembly";
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", "MyAssembly.dll");
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called MyMethod.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod",MethodAttributes.Public | MethodAttributes.HideBySig |
                                                                           MethodAttributes.Static, typeof(string), new Type[] {typeof(int), typeof(short), typeof(long)});
      // Set the attributes for the parameters of the method.
      // Set the attribute for the first parameter to IN.
      ParameterBuilder myParameterBuilder = myMethodBuilder.DefineParameter(1, ParameterAttributes.In, "MyIntParameter");
      // Set the attribute for the second parameter to OUT.
      myParameterBuilder = myMethodBuilder.DefineParameter(2, ParameterAttributes.Out, "MyShortParameter");
      // Set the attribute for the third parameter to OPTIONAL.
      myParameterBuilder = myMethodBuilder.DefineParameter(3, ParameterAttributes.Optional | ParameterAttributes.HasDefault, "MyLongParameter");
      // Get the Microsoft Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the MSIL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the "ret" MSIL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
   }

   public static void Main()
   {
      // Create a dynamic assembly with a type named MyType.
      DefineMethod();

      // Get the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();
      Assembly myAssembly = null;
      // Get the assembly named MyAssembly.
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName(false).Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      if(myAssembly != null)
      {
         // Get a type named MyType.
         Type myType = myAssembly.GetType("MyType");
         // Get a 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 :", 
            myMethodBase, myParameters.Length);
         // Print the IN, OUT and OPTIONAL attributes associated with each of the parameters.
         for(int i = 0; i < myParameters.Length; i++)
         {
            if(myParameters[i].IsIn)
               Console.WriteLine("\tThe {0} parameter has the In attribute", 
                                       i + 1);
            if(myParameters[i].IsOptional)
               Console.WriteLine("\tThe {0} parameter has the Optional attribute",
                                       i + 1);
            if(myParameters[i].IsOut)
               Console.WriteLine("\tThe {0} parameter has the Out attribute",
                                       i + 1);
         }
      }
      else
         Console.WriteLine("Could not find a assembly named 'MyAssembly' for the current application domain");
   }
}

    

Return to top


Property: IsLcid (read-only)
Summary
Gets a value indicating whether this parameter is a locale identifier (lcid).
C# Syntax:
public bool IsLcid {get;}
Remarks
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the Lcid flag of the ParameterAttributes enumerator.

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

Example

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class ParameterInfo_IsIn_IsOut_IsOptional
{
   public static void DefineMethod()
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "MyAssembly";
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", "MyAssembly.dll");
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called MyMethod.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod",MethodAttributes.Public | MethodAttributes.HideBySig |
                                                                           MethodAttributes.Static, typeof(string), new Type[] {typeof(int), typeof(short), typeof(long)});
      // Set the attributes for the parameters of the method.
      // Set the attribute for the first parameter to IN.
      ParameterBuilder myParameterBuilder = myMethodBuilder.DefineParameter(1, ParameterAttributes.In, "MyIntParameter");
      // Set the attribute for the second parameter to OUT.
      myParameterBuilder = myMethodBuilder.DefineParameter(2, ParameterAttributes.Out, "MyShortParameter");
      // Set the attribute for the third parameter to OPTIONAL.
      myParameterBuilder = myMethodBuilder.DefineParameter(3, ParameterAttributes.Optional | ParameterAttributes.HasDefault, "MyLongParameter");
      // Get the Microsoft Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the MSIL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the "ret" MSIL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
   }

   public static void Main()
   {
      // Create a dynamic assembly with a type named MyType.
      DefineMethod();

      // Get the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();
      Assembly myAssembly = null;
      // Get the assembly named MyAssembly.
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName(false).Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      if(myAssembly != null)
      {
         // Get a type named MyType.
         Type myType = myAssembly.GetType("MyType");
         // Get a 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 :", 
            myMethodBase, myParameters.Length);
         // Print the IN, OUT and OPTIONAL attributes associated with each of the parameters.
         for(int i = 0; i < myParameters.Length; i++)
         {
            if(myParameters[i].IsIn)
               Console.WriteLine("\tThe {0} parameter has the In attribute", 
                                       i + 1);
            if(myParameters[i].IsOptional)
               Console.WriteLine("\tThe {0} parameter has the Optional attribute",
                                       i + 1);
            if(myParameters[i].IsOut)
               Console.WriteLine("\tThe {0} parameter has the Out attribute",
                                       i + 1);
         }
      }
      else
         Console.WriteLine("Could not find a assembly named 'MyAssembly' for the current application domain");
   }
}

    

Return to top


Property: IsOptional (read-only)
Summary
Gets a value indicating whether this parameter is optional.
C# Syntax:
public bool IsOptional {get;}
Remarks
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the Optional flag of the ParameterAttributes enumerator.

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

Example

using System;
using System.Reflection;
using System.Threading;
using System.Reflection.Emit;

public class ParameterInfo_IsIn_IsOut_IsOptional
{
   public static void DefineMethod()
   {
      AssemblyName myAssemblyName = new AssemblyName();
      myAssemblyName.Name = "MyAssembly";
      // Get the assembly builder from the application domain associated with the current thread.
      AssemblyBuilder myAssemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
      // Create a dynamic module in the assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.DefineDynamicModule("MyModule", "MyAssembly.dll");
      // Create a type in the module.
      TypeBuilder myTypeBuilder = myModuleBuilder.DefineType("MyType");
      // Create a method called MyMethod.
      MethodBuilder myMethodBuilder = myTypeBuilder.DefineMethod("MyMethod",MethodAttributes.Public | MethodAttributes.HideBySig |
                                                                           MethodAttributes.Static, typeof(string), new Type[] {typeof(int), typeof(short), typeof(long)});
      // Set the attributes for the parameters of the method.
      // Set the attribute for the first parameter to IN.
      ParameterBuilder myParameterBuilder = myMethodBuilder.DefineParameter(1, ParameterAttributes.In, "MyIntParameter");
      // Set the attribute for the second parameter to OUT.
      myParameterBuilder = myMethodBuilder.DefineParameter(2, ParameterAttributes.Out, "MyShortParameter");
      // Set the attribute for the third parameter to OPTIONAL.
      myParameterBuilder = myMethodBuilder.DefineParameter(3, ParameterAttributes.Optional | ParameterAttributes.HasDefault, "MyLongParameter");
      // Get the Microsoft Intermediate Language generator for the method.
      ILGenerator myILGenerator = myMethodBuilder.GetILGenerator();
      // Use the utility method to generate the MSIL instructions that print a string to the console.
      myILGenerator.EmitWriteLine("Hello World!");
      // Generate the "ret" MSIL instruction.
      myILGenerator.Emit(OpCodes.Ret);
      // End the creation of the type.
      myTypeBuilder.CreateType();
   }

   public static void Main()
   {
      // Create a dynamic assembly with a type named MyType.
      DefineMethod();

      // Get the assemblies currently loaded in the application domain.
      Assembly[] myAssemblies = Thread.GetDomain().GetAssemblies();
      Assembly myAssembly = null;
      // Get the assembly named MyAssembly.
      for(int i = 0; i < myAssemblies.Length; i++)
         if(String.Compare(myAssemblies[i].GetName(false).Name, "MyAssembly") == 0)
            myAssembly = myAssemblies[i];

      if(myAssembly != null)
      {
         // Get a type named MyType.
         Type myType = myAssembly.GetType("MyType");
         // Get a 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 :", 
            myMethodBase, myParameters.Length);
         // Print the IN, OUT and OPTIONAL attributes associated with each of the parameters.
         for(int i = 0; i < myParameters.Length; i++)
         {
            if(myParameters[i].IsIn)
               Console.WriteLine("\tThe {0} parameter has the In attribute", 
                                       i + 1);
            if(myParameters[i].IsOptional)
               Console.WriteLine("\tThe {0} parameter has the Optional attribute",
                                       i + 1);
            if(myParameters[i].IsOut)
               Console.WriteLine("\tThe {0} parameter has the Out attribute",
                                       i + 1);
         }
      }
      else
         Console.WriteLine("Could not find a assembly named 'MyAssembly' for the current application domain");
   }
}

    

Return to top


Property: IsOut (read-only)
Summary
Gets a value indicating whether this is an output parameter.
C# Syntax:
public bool IsOut {get;}
Remarks
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the Out flag of the ParameterAttributes enumerator.

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

Example
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 IsOut of each parameter.
       foreach (ParameterInfo Myparam in Myarray)
       {
          Console.Write ("\nFor parameter # "   + Myparam.Position 
             + ", the IsOut is - " +  Myparam.IsOut );
       }
       return 0;
    }
 }
 /*
 This code produces the following output:

 Reflection.ParameterInfo
  
 Mymethodbase = Void mymethod (Int32, System.String ByRef, System.String ByRef)
 For parameter # 0, the IsOut is - False
 For parameter # 1, the IsOut is - True
 For parameter # 2, the IsOut is - False
 */

    

Return to top


Property: IsRetval (read-only)
Summary
Gets a value indicating whether this is a Retval parameter.
C# Syntax:
public bool IsRetval {get;}
Remarks
This method depends on an optional metadata flag. This flag can be inserted by compilers, but the compilers are not obligated to do so.

This method utilizes the Retval flag of the ParameterAttributes enumerator.

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

Return to top


Property: Member (read-only)
Summary
Gets a value indicating the member in which the parameter is implemented.
C# Syntax:
public virtual MemberInfo Member {get;}

Return to top


Property: Name (read-only)
Summary
Gets the name of the parameter.
C# Syntax:
public virtual string Name {get;}
Remarks
This method utilizes the protected ParameterInfo.NameImpl method, and depends on an optional metadata flag that might not be available in all compilers.

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

Example
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 (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
 */

    

Return to top


Property: ParameterType (read-only)
Summary
Gets the Type of this parameter.
C# Syntax:
public virtual Type ParameterType {get;}
Remarks
This method depends on an optional metadata and might not be available in all compilers.

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

Example
 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(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&
 */

    

Return to top


Property: Position (read-only)
Summary
Gets the signature position for the parameter.
C# Syntax:
public virtual int Position {get;}
Remarks
This method utilizes the ParameterInfo.PositionImpl method.

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

See also:
ParameterInfo.Name

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~ParameterInfo();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: GetCustomAttributes(
   bool inherit
)
Summary
Gets all the custom attributes defined on this parameter.
C# Syntax:
public virtual object[] GetCustomAttributes(
   bool inherit
);
Parameters:

inherit

This argument is ignored for object of this type.

Return Value:
An array of type Object containing all the custom attributes defined on this parameter.
Implements:
ICustomAttributeProvider.GetCustomAttributes
Example

using System;
using System.Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
   private string myName;
   public MyAttribute(string name)
   {
      myName = name;
   }
   public string Name 
   {
      get 
      {
         return myName;
      }
   }
}

// Define a class which has a custom attribute associated with one of parameters of a method. 
public class MyClass1
{
   public void MyMethod(
      [MyAttribute("This is an example parameter attribute")]
      int i)
   {
      return;
   }
}

public class MemberInfo_GetCustomAttributes 
{
   public static void Main()
   {
      // Get the type of the class 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the members associated with the class 'MyClass1'.
      MethodInfo[] myMethods = myType.GetMethods();

      // Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
      for(int i = 0; i < myMethods.Length; i++)
      {
         // Get the parameters for the method.
         ParameterInfo[] myParameters = myMethods[i].GetParameters();
         Console.WriteLine("\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[i]);
         for(int j = 0; j < myParameters.Length; j++)
         {
            // Get the attributes of type 'MyAttribute' for each parameter.
            Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);
            if(myParameters[j].IsDefined(typeof(MyAttribute), false))
            {
               Console.WriteLine("\nThe attributes for the parameter \"{0}\" are : \n", myParameters[j].ParameterType);
               // Display all the attributes of type 'MyAttribute' for a parameter.
               for(int k = 0; k < myAttributes.Length; k++)
                  Console.WriteLine("The type of the attribute is : {0}", myAttributes[k]);
            }
         }
      }  
   }
}


    

Return to top


Overloaded Method: GetCustomAttributes(
   Type attributeType,
   bool inherit
)
Summary
Gets the custom attributes of the specified type defined on this parameter.
C# Syntax:
public virtual object[] GetCustomAttributes(
   Type attributeType,
   bool inherit
);
Parameters:

attributeType

The custom attributes identified by type.

inherit

This argument is ignored for objects of this type.

Return Value:
An array of type Object containing the custom attributes of the specified type.
Implements:
ICustomAttributeProvider.GetCustomAttributes

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: IsDefined(
   Type attributeType,
   bool inherit
)
Summary
Determines if the custom attribute of the specified type is defined on this member.
C# Syntax:
public virtual bool IsDefined(
   Type attributeType,
   bool inherit
);
Parameters:

attributeType

The Type object to search for.

inherit

This argument is ignored for objects of this type.

Return Value:
true if one or more instance of attributeType is defined on this member; otherwise, false.
Exceptions
Exception Type Condition
ArgumentNullException attributeType is null.
Implements:
ICustomAttributeProvider.IsDefined
Example

using System;
using System.Reflection;

// Define a custom attribute with one named parameter.
[AttributeUsage(AttributeTargets.Parameter)]
public class MyAttribute : Attribute
{
   private string myName;
   public MyAttribute(string name)
   {
      myName = name;
   }
   public string Name 
   {
      get 
      {
         return myName;
      }
   }
}

// Define a class which has a custom attribute associated with one of parameters of a method. 
public class MyClass1
{
   public void MyMethod(
      [MyAttribute("This is an example parameter attribute")]
      int i)
   {
      return;
   }
}

public class MemberInfo_GetCustomAttributes 
{
   public static void Main()
   {
      // Get the type of the class 'MyClass1'.
      Type myType = typeof(MyClass1);
      // Get the members associated with the class 'MyClass1'.
      MethodInfo[] myMethods = myType.GetMethods();

      // Display the attributes of type 'MyAttribute' for each of the parameters of each method of the class 'MyClass1'.
      for(int i = 0; i < myMethods.Length; i++)
      {
         // Get the parameters for the method.
         ParameterInfo[] myParameters = myMethods[i].GetParameters();
         Console.WriteLine("\nThe parameters for the method \"{0}\" which have an custom attribute are : \n", myMethods[i]);
         for(int j = 0; j < myParameters.Length; j++)
         {
            // Get the attributes of type 'MyAttribute' for each parameter.
            Object[] myAttributes = myParameters[j].GetCustomAttributes(typeof(MyAttribute), false);
            if(myParameters[j].IsDefined(typeof(MyAttribute), false))
            {
               Console.WriteLine("\nThe attributes for the parameter \"{0}\" are : \n", myParameters[j].ParameterType);
               // Display all the attributes of type 'MyAttribute' for a parameter.
               for(int k = 0; k < myAttributes.Length; k++)
                  Console.WriteLine("The type of the attribute is : {0}", myAttributes[k]);
            }
         }
      }  
   }
}


    

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.