class Person { public void SayHello() { Console.WriteLine("Hello there!"); } } ... Type type = typeof(Person); MethodInfo methodInfo = type.GetMethod("SayHello"); Console.WriteLine(methodInfo.Name); // Output: SayHello
Person person = new Person(); Type type = typeof(Person); MethodInfo methodInfo = type.GetMethod("SayHello"); methodInfo.Invoke(person, null); // Output: Hello there!In this example, we create an instance of the Person class and store it in the person variable. We then use the typeof operator and the GetMethod method to obtain a MethodInfo object representing the "SayHello" method of the class. Finally, we use the Invoke method of the MethodInfo object to call the method on the person object. Package Library: System.Reflection.MethodInfo belongs to the .NET Framework Class Library.