Invoke methods using reflection : Method « Reflection « C# / CSharp Tutorial






using System; 
using System.Reflection; 
 
class MyClass { 

 
  public MyClass(int i, int j) { 

    Console.WriteLine("MyClass(int i, int j)");
    Console.WriteLine(i);
    Console.WriteLine(j);
  } 
 
  public int sum() { 
    Console.WriteLine("sum");
    return 0; 
  } 
 
  public bool isBetween(int i) { 
    Console.WriteLine("isBetween");
    return false; 
  } 
 
  public void set(int a, int b) { 
    Console.Write("Inside set(int, int). "); 
  } 
 
  public void set(double a, double b) { 
    Console.Write("Inside set(double, double). "); 
  } 
 
  public void show() { 
    Console.WriteLine("show"); 
  } 
} 
 
class MainClass { 
  public static void Main() { 
    Type t = typeof(MyClass);   
    MyClass reflectOb = new MyClass(10, 20); 
    int val; 
 
    Console.WriteLine("Invoking methods in " + t.Name);     
    Console.WriteLine(); 
    MethodInfo[] mi = t.GetMethods(); 
 
    // Invoke each method. 
    foreach(MethodInfo m in mi) { 
      // Get the parameters. 
      ParameterInfo[] pi = m.GetParameters(); 
 
      if(m.Name.CompareTo("set")==0 && 
         pi[0].ParameterType == typeof(int)) { 
        object[] args = new object[2]; 
        args[0] = 9; 
        args[1] = 18; 
        m.Invoke(reflectOb, args); 
      } 
      else if(m.Name.CompareTo("set")==0 && 
         pi[0].ParameterType == typeof(double)) { 
        object[] args = new object[2]; 
        args[0] = 1.12; 
        args[1] = 23.4; 
        m.Invoke(reflectOb, args); 
      } 
      else if(m.Name.CompareTo("sum")==0) { 
        val = (int) m.Invoke(reflectOb, null); 
        Console.WriteLine("sum is " + val); 
      } 
      else if(m.Name.CompareTo("isBetween")==0) { 
        object[] args = new object[1]; 
        args[0] = 14; 
        if((bool) m.Invoke(reflectOb, args)) 
          Console.WriteLine("14 is between x and y"); 
      } 
      else if(m.Name.CompareTo("show")==0) { 
        m.Invoke(reflectOb, null); 
      } 
    } 
  } 
}
MyClass(int i, int j)
10
20
Invoking methods in MyClass

sum
sum is 0
isBetween
Inside set(int, int). Inside set(double, double). show








19.5.Method
19.5.1.Deeper Reflection: iterate through the methods of the class
19.5.2.Deeper Reflection: Invoking Functions
19.5.3.Invoke methods using reflection
19.5.4.List Methods
19.5.5.Obtain the metadata tokens
19.5.6.Inspecting a method's body
19.5.7.Dynamically Invoking A Method
19.5.8.Finding Particular Members
19.5.9.Call string method with reflection and Expression
19.5.10.Invoke member method with created instance from reflection
19.5.11.Use reflection to invoke methods
19.5.12.Use DefinePInvokeMethod method to create a PInvoke method.
19.5.13.Get the method that matches the specified binding flags.