Home   Cover Cover Cover Cover
 

Dynamic method invocation

Chap4_Ex20.cs
using System;
using System.Reflection;

public class DynObjCreation {
  
  public static void Main(string[] args) {
    Assembly a = Assembly.LoadFrom("TestReflection.dll");
    object trObj = a.CreateInstance("TestReflection");
    Type t = a.GetType("TestReflection");
    MethodInfo mi = t.GetMethod("WithoutParameters");
    MethodInfo mi2 = t.GetMethod("WithParameters");
    mi.Invoke(trObj,null);
    object[] p = new object[1];
    p[0] = "hello";
    mi2.Invoke(trObj,p);
}
}