[Serializable] |
MethodBase.GetParameters returns an array of ParameterInfo objects representing the parameters of a method, in order.
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. |
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. |
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. |
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. |
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:
protected ParameterInfo(); |
protected ParameterAttributes AttrsImpl;
|
Typical access to parameter attributes is through ParameterInfo.Attributes.
protected Type ClassImpl;
|
Typical access to parameter types is through ParameterInfo.ParameterType.
protected object DefaultValueImpl;
|
Typical access to the default value of the parameter is through ParameterInfo.DefaultValue.
protected MemberInfo MemberImpl;
|
Typical access to the parameter name is through the ParameterInfo.Member.
protected string NameImpl;
|
Typical access to the parameter name is through the ParameterInfo.Name.
protected int PositionImpl;
|
Typical access to the name of the parameter is through ParameterInfo.Position.
public virtual ParameterAttributes Attributes {get;}
|
To get the ParameterInfo array, first get the method or the constructor and then call MethodBase.GetParameters.
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); } }
public virtual object DefaultValue {get;}
|
This method utilizes the ParameterInfo.DefaultValueImpl method.
To get the ParameterInfo array, first get the method or the constructor and then call MethodBase.GetParameters.
public bool IsIn {get;}
|
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.
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"); } }
public bool IsLcid {get;}
|
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.
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"); } }
public bool IsOptional {get;}
|
This method utilizes the Optional flag of the ParameterAttributes enumerator.
To get the ParameterInfo array, first get the method and then call MethodBase.GetParameters.
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"); } }
public bool IsOut {get;}
|
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.
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 */
public bool IsRetval {get;}
|
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.
public virtual MemberInfo Member {get;}
|
public virtual string Name {get;}
|
To get the ParameterInfo array, first get the method or the constructor and then call MethodBase.GetParameters.
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 */
public virtual Type ParameterType {get;}
|
To get the ParameterInfo array, first get the method or the constructor and then call MethodBase.GetParameters.
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& */
public virtual int Position {get;}
|
To get the ParameterInfo array, first get the method or the constructor and then call MethodBase.GetParameters.
~ParameterInfo(); |
inherit
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]); } } } } }
attributeType
inherit
public virtual int GetHashCode(); |
public Type GetType(); |
attributeType
inherit
Exception Type | Condition |
---|---|
ArgumentNullException | attributeType is null. |
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]); } } } } }
protected object MemberwiseClone(); |
public virtual string ToString(); |