16.3. Retrieving methods from a type

You can retrieve a single MethodInfo object, if you already know the name of the method, or an array of MethodInfo objects.

In the following example, MyClass contains a method called DoThis() which takes in no parameters. Some useful methods of MethodInfo are demonstrated.

 1: using System;
 2: using System.Reflection;
 3:
 4: public class MyClass{
 5:   public static void DoThis(){
 6:     Console.WriteLine("doing this");
 7:   }
 8:   public static void DoThat(){
 9:     Console.WriteLine("doing that");
10:   }
11: }
12:
13: public class TestClass{
14:   public static void Main(){
15:
16:     Type t = Type.GetType("MyClass");
17:     MethodInfo m = t.GetMethod("DoThis"); 18: Console.WriteLine("method :" + m); 19: Console.WriteLine("Name ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.