public static void invokeTypeMemberDinamically() { // Create an instance of this type. dynamic myInstance = new FakeClass(); // Call the method dynamically. myInstance.printMessage2("hello", 37, 'c'); Console.WriteLine("\nMain method complete. Press Enter."); Console.ReadLine(); }
public static void invokeTypeMemberUsingReflection() { // Create an instance of this type. object myInstance = new FakeClass(); // Get the type we are interested in. Type myType = typeof(FakeClass); // Get the method information. MethodInfo methodInfo = myType.GetMethod("printMessage", new Type[] { typeof(string), typeof(int), typeof(char) }); // Invoke the method using the instance we created. myType.InvokeMember("printMessage", BindingFlags.InvokeMethod, null, myInstance, new object[] { "hello", 37, 'c' }); methodInfo.Invoke(null, BindingFlags.InvokeMethod, null, new object[] { "hello", 37, 'c' }, null); Console.WriteLine("\nMain method complete. Press Enter."); Console.ReadLine(); }