ParameterInfo Class
System.ReflectionRepresents a parameter of a method or constructor. This class cannot be inherited.
Summary
public sealed class ParameterInfo
Members
Properties
-
Attributes
public ParameterAttributes Attributes { get; }Gets the attributes of the parameter.
-
DefaultValue
public object DefaultValue { get; }Gets the default value of the parameter.
-
IsOptional
public bool IsOptional { get; }Gets a value indicating whether the parameter is optional.
-
IsOut
public bool IsOut { get; }Gets a value indicating whether the parameter is an output parameter.
-
IsRetval
public bool IsRetval { get; }Gets a value indicating whether the parameter is a return value parameter.
-
Member
public System.Reflection.MemberInfo Member { get; }Gets the method or constructor that contains this parameter.
-
Name
public string Name { get; }Gets the name of the parameter.
-
ParameterType
public Type ParameterType { get; }Gets the type of the parameter.
-
Position
public int Position { get; }Gets the zero-based position of the parameter in the constructor or method signature.
Methods
-
Equals(object obj)
public override bool Equals(object obj)Determines whether the specified object is equal to the current object.
-
GetCustomAttributes(Type attributeType, bool inherit)
public object[] GetCustomAttributes(Type attributeType, bool inherit)When overridden in a derived class, searches for a specified custom attribute defined on the parameter.
-
GetCustomAttributes(bool inherit)
public object[] GetCustomAttributes(bool inherit)Gets a collection of custom attributes that are applied to the parameter.
-
GetHashCode()
public override int GetHashCode()Serves as the default hash function.
-
GetType()
public Type GetType()Gets the runtime type of the current instance.
-
IsDefined(Type attributeType, bool inherit)
public bool IsDefined(Type attributeType, bool inherit)Determines whether a custom attribute of a specified type is applied to the parameter.
-
ToString()
public override string ToString()Returns a string that represents the current object.
Example Usage
using System;
using System.Reflection;
public class Example
{
public void MyMethod(string name, int count = 1)
{
}
public static void Main(string[] args)
{
MethodInfo method = typeof(Example).GetMethod("MyMethod");
ParameterInfo[] parameters = method.GetParameters();
foreach (ParameterInfo param in parameters)
{
Console.WriteLine($"Parameter Name: {param.Name}");
Console.WriteLine($"Parameter Type: {param.ParameterType.Name}");
Console.WriteLine($"Is Optional: {param.IsOptional}");
if (param.IsOptional)
{
Console.WriteLine($"Default Value: {param.DefaultValue}");
}
Console.WriteLine("---");
}
}
}
Output of the Example
Parameter Name: name
Parameter Type: String
Is Optional: False
---
Parameter Name: count
Parameter Type: Int32
Is Optional: True
Default Value: 1
---