Represents a method on a type. This class is used for reflection. The MethodInfo class provides information about a method, such as its name, return type, parameters, and attributes. It allows you to invoke the method dynamically.
Provides members that represent a method or constructor. Use the static methods of this class to create MethodInfo objects.
The following example demonstrates how to use MethodInfo to get information about a method and invoke it.
using System; using System.Reflection; public class Example { public void Greet(string name) { Console.WriteLine($"Hello, {name}!"); } public static void Main(string[] args) { Type exampleType = typeof(Example); MethodInfo greetMethod = exampleType.GetMethod("Greet"); if (greetMethod != null) { Console.WriteLine($"Method Name: {greetMethod.Name}"); Console.WriteLine($"Return Type: {greetMethod.ReturnType}"); // Create an instance of the class Example instance = new Example(); // Define parameters object[] parameters = { "World" }; // Invoke the method greetMethod.Invoke(instance, parameters); } } }