System.Reflection
Represents a parameter of a method or constructor. This class cannot be inherited.
The ParameterInfo
class is a core component of the .NET Reflection API. It provides access to metadata about a method or constructor parameter, such as its name, type, default value, attributes, and position within the parameter list.
System.Object
System.Reflection.ParameterInfo
MemberInfo Member { get; }
string Name { get; }
Type ParameterType { get; }
int Position { get; }
object DefaultValue { get; }
ParameterAttributes Attributes { get; }
bool IsOut { get; }
out
keyword.bool IsOptional { get; }
bool IsIn { get; }
in
keyword.bool IsLcid { get; }
lcid
attribute.bool IsRetval { get; }
bool HasDefaultValue { get; }
You can obtain ParameterInfo
objects by invoking the GetParameters()
method on a MethodInfo
or ConstructorInfo
object.
using System;
using System.Reflection;
public class Example
{
public static void SampleMethod(int count, string name = "Default", bool flag = true) { }
public static void Main(string[] args)
{
MethodInfo method = typeof(Example).GetMethod("SampleMethod");
ParameterInfo[] parameters = method.GetParameters();
Console.WriteLine($"Parameters for {method.Name}:");
foreach (ParameterInfo parameter in parameters)
{
Console.WriteLine("--------------------");
Console.WriteLine($"Name: {parameter.Name}");
Console.WriteLine($"Type: {parameter.ParameterType.Name}");
Console.WriteLine($"Position: {parameter.Position}");
Console.WriteLine($"IsOptional: {parameter.IsOptional}");
Console.WriteLine($"HasDefaultValue: {parameter.HasDefaultValue}");
if (parameter.HasDefaultValue)
{
Console.WriteLine($"DefaultValue: {parameter.DefaultValue}");
}
Console.WriteLine($"Attributes: {parameter.Attributes}");
}
}
}
Parameters for SampleMethod:
--------------------
Name: count
Type: Int32
Position: 0
IsOptional: False
HasDefaultValue: False
Attributes: None
--------------------
Name: name
Type: String
Position: 1
IsOptional: True
HasDefaultValue: True
DefaultValue: Default
Attributes: Optional
--------------------
Name: flag
Type: Boolean
Position: 2
IsOptional: True
HasDefaultValue: True
DefaultValue: True
Attributes: Optional
Namespace: System.Reflection
Assembly: System.Private.CoreLib (in System.Private.CoreLib.dll)