ParameterInfo Class

System.Reflection

Represents a parameter of a method or constructor. This class cannot be inherited.

Summary

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.

Inheritance Hierarchy

System.Object
    System.Reflection.ParameterInfo

Members

Remarks

You can obtain ParameterInfo objects by invoking the GetParameters() method on a MethodInfo or ConstructorInfo object.

Example


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

Example Output:


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
            

Requirements

Namespace: System.Reflection

Assembly: System.Private.CoreLib (in System.Private.CoreLib.dll)