CSharp examples for Reflection:Member
Invoke a Type Member Using Reflection
using System;/*from w ww . j av a2s. c o m*/ using System.Reflection; class MainClass { static void Main(string[] args) { object myInstance = new MainClass(); // get the type we are interested in Type myType = typeof(MainClass); // get the method information MethodInfo methodInfo = myType.GetMethod("printMessage", new Type[] { typeof(string), typeof(int), typeof(char) }); // invoke the method through the methodinfo methodInfo.Invoke(myInstance, new object[] { "hello", 37, 'c' }); // 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); // Wait to continue. Console.WriteLine("\nMain method complete. Press Enter."); Console.ReadLine(); } public static void printMessage(string param1, int param2, char param3) { Console.WriteLine("PrintMessage {0} {1} {2}", param1, param2, param3); } }