O'Reilly logo

From Java to C#: A Developer's Guide by Heng Ngee Mok

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required