MethodInfo method = typeof(MyClass).GetMethod("MyMethod"); ParameterInfo[] parameters = method.GetParameters(); foreach(ParameterInfo p in parameters) { Console.WriteLine("{0} ({1})", p.Name, p.ParameterType); }
public void MyMethod(string name, int age) { } MethodInfo method = typeof(MyClass).GetMethod("MyMethod"); ParameterInfo[] parameters = method.GetParameters(); foreach(ParameterInfo p in parameters) { Console.WriteLine("{0} ({1})", p.Name, p.ParameterType); Console.WriteLine("Is optional? {0}", p.IsOptional); Console.WriteLine("Has default value? {0}", p.HasDefaultValue); }In this example, we retrieve the MethodInfo for a method called "MyMethod" in the MyClass class, which has two parameters - a string called "name" and an integer called "age". We use the GetParameters() method to retrieve an array of ParameterInfo objects for the method's parameters. Then, we loop through the array and print out the name and type of each parameter, as well as whether or not it is optional and has a default value. The System.Reflection namespace is included in the .NET Framework Class Library.