將MethodInfo轉(zhuǎn)化為Delegate的方式
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace MethodInfoInvokeDemo { public class ReflectTest { public void MethodWithNoParaNoReturn() { Console.WriteLine("不帶參數(shù)且不返回值的方法"); } public string MethodWithNoPara() { Console.WriteLine("不帶參數(shù)且有返回值的方法"); return "MethodWithNoPara"; } public string Method1(string str) { Console.WriteLine("帶參數(shù)且有返回值的方法"); return str; } public string Method2(string str, int index) { Console.WriteLine("帶參數(shù)且有返回值的方法"); return str + index.ToString(); } public string Method3(string str, out string outStr) { outStr = "bbbb"; Console.WriteLine("帶參數(shù)且有返回值的方法"); return str; } public static string StaticMethod() { Console.WriteLine("靜態(tài)方法"); return "cccc"; } } class Program { static void Main(string[] args) { Type type = typeof(ReflectTest); object reflectTest = Activator.CreateInstance(type); //不帶參數(shù)且不返回值的方法的調(diào)用 MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn"); methodInfo.Invoke(reflectTest, null); Console.WriteLine(); //不帶參數(shù)且有返回值的方法的調(diào)用 methodInfo = type.GetMethod("MethodWithNoPara"); Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString()); Console.WriteLine(); //帶參數(shù)且有返回值的方法的調(diào)用 methodInfo = type.GetMethod("Method1", new Type[]{typeof(string)}); Console.WriteLine(methodInfo.Invoke(reflectTest, new object[]{"測試"}).ToString()); Console.WriteLine(); //帶多個參數(shù)且有返回值的方法的調(diào)用 methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) }); Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "測試", 100 }).ToString()); //Console.WriteLine(); //methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) }); //string outStr = ""; //Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "測試", outStr }).ToString()); Console.WriteLine(); //靜態(tài)方法的調(diào)用 methodInfo = type.GetMethod("StaticMethod"); Console.WriteLine(methodInfo.Invoke(null, null).ToString()); Console.ReadKey(); } } }
有時再用反射的時候,需要將反射出的方法注冊給某個事件,這是就需要將改方法轉(zhuǎn)化為delegate后才能綁定到對應(yīng)的事件上
可以通過Delegate.CreateDelegate的方法來實現(xiàn),如下:
/// <summary> /// 生成反射過來的MethodInfo到指定類型的委托 /// </summary> /// <typeparam name="T">EventArgs泛型類型</typeparam> /// <param name="instance">當(dāng)前對象</param> /// <param name="method">需要轉(zhuǎn)化為delegate的方法</param> /// <returns></returns> public static Delegate CreateDelegateFromMethodInfo<T>(Object instance,MethodInfo method) where T:EventArgs//約束泛型T只能是來自EventArgs類型的 { Delegate del = Delegate.CreateDelegate(typeof(EventHandler<T>), instance, method); EventHandler<T> mymethod = del as EventHandler<T>; return mymethod; } /// <summary> /// 生成反射過來的MethodInfo到指定類型的委托 /// </summary> /// <typeparam name="T">EventHandle泛型類型</typeparam> /// <param name="instance">當(dāng)前對象</param> /// <param name="method">需要轉(zhuǎn)化為delegate的方法</param> /// <returns></returns> public static Delegate CreateDelegateFromMethodInfoByDelegate<T>(Object instance, MethodInfo method) { Delegate del = Delegate.CreateDelegate(typeof(T), instance, method); EventHandler mymethod = del as EventHandler; return mymethod; }
private void button3_Click(object sender, EventArgs e) { var Instance = Activator.CreateInstance(typeof(Form1)) as Form1; MethodInfo methodInfo = typeof(Form1).GetMethod("Method1"); EventHandler dd = Delegate.CreateDelegate(typeof(EventHandler), Instance,methodInfo) as EventHandler; dd(null, null); } public void Method1(object sender, EventArgs e) { MessageBox.Show("ee"); }
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答?。?點擊進入論壇