您现在的位置: 365建站网 > 365文章 > C#中MethodInfo使用方法和实例(转化为Delegate)

C#中MethodInfo使用方法和实例(转化为Delegate)

文章来源:365jz.com     点击数:1503    更新时间:2018-06-08 22:46   参与评论

将MethodInfo转化为Delegate的方式

</>code

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;
  6. namespace MethodInfoInvokeDemo
  7. {
  8. public class ReflectTest
  9. {
  10. public void MethodWithNoParaNoReturn()
  11. {
  12. Console.WriteLine("不带参数且不返回值的方法");
  13. }
  14. public string MethodWithNoPara()
  15. {
  16. Console.WriteLine("不带参数且有返回值的方法");
  17. return "MethodWithNoPara";
  18. }
  19. public string Method1(string str)
  20. {
  21. Console.WriteLine("带参数且有返回值的方法");
  22. return str;
  23. }
  24. public string Method2(string str, int index)
  25. {
  26. Console.WriteLine("带参数且有返回值的方法");
  27. return str + index.ToString();
  28. }
  29. public string Method3(string str, out string outStr)
  30. {
  31. outStr = "bbbb";
  32. Console.WriteLine("带参数且有返回值的方法");
  33. return str;
  34. }
  35. public static string StaticMethod()
  36. {
  37. Console.WriteLine("静态方法");
  38. return "cccc";
  39. }
  40. }
  41. class Program
  42. {
  43. static void Main(string[] args)
  44. {
  45. Type type = typeof(ReflectTest);
  46. object reflectTest = Activator.CreateInstance(type);
  47. //不带参数且不返回值的方法的调用
  48. MethodInfo methodInfo = type.GetMethod("MethodWithNoParaNoReturn");
  49. methodInfo.Invoke(reflectTest, null);
  50. Console.WriteLine();
  51. //不带参数且有返回值的方法的调用
  52. methodInfo = type.GetMethod("MethodWithNoPara");
  53. Console.WriteLine(methodInfo.Invoke(reflectTest, null).ToString());
  54. Console.WriteLine();
  55. //带参数且有返回值的方法的调用
  56. methodInfo = type.GetMethod("Method1", new Type[]{typeof(string)});
  57. Console.WriteLine(methodInfo.Invoke(reflectTest, new object[]{"测试"}).ToString());
  58. Console.WriteLine();
  59. //带多个参数且有返回值的方法的调用
  60. methodInfo = type.GetMethod("Method2", new Type[] { typeof(string), typeof(int) });
  61. Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", 100 }).ToString());
  62. //Console.WriteLine();
  63. //methodInfo = type.GetMethod("Method3", new Type[] { typeof(string), typeof(string) });
  64. //string outStr = "";
  65. //Console.WriteLine(methodInfo.Invoke(reflectTest, new object[] { "测试", outStr }).ToString());
  66. Console.WriteLine();
  67. //静态方法的调用
  68. methodInfo = type.GetMethod("StaticMethod"); 
  69. Console.WriteLine(methodInfo.Invoke(null, null).ToString());
  70. Console.ReadKey();
  71. }
  72. }
  73. }


有时再用反射的时候,需要将反射出的方法注册给某个事件,这是就需要将改方法转化为delegate后才能绑定到对应的事件上

可以通过Delegate.CreateDelegate的方法来实现,如下:

</>code

  1. /// <summary>    
  2.         /// 生成反射过来的MethodInfo到指定类型的委托    
  3.         /// </summary>    
  4.         /// <typeparam name="T">EventArgs泛型类型</typeparam>    
  5.         /// <param name="instance">当前对象</param>    
  6.         /// <param name="method">需要转化为delegate的方法</param>    
  7.         /// <returns></returns>    
  8.         public static Delegate CreateDelegateFromMethodInfo<T>(Object instance,MethodInfo method) where T:EventArgs//约束泛型T只能是来自EventArgs类型的    
  9.         {    
  10.     
  11.             Delegate del = Delegate.CreateDelegate(typeof(EventHandler<T>), instance, method);    
  12.             EventHandler<T> mymethod = del as EventHandler<T>;    
  13.             return mymethod;    
  14.         }    
  15.     
  16.         /// <summary>    
  17.         /// 生成反射过来的MethodInfo到指定类型的委托    
  18.         /// </summary>    
  19.         /// <typeparam name="T">EventHandle泛型类型</typeparam>    
  20.         /// <param name="instance">当前对象</param>    
  21.         /// <param name="method">需要转化为delegate的方法</param>    
  22.         /// <returns></returns>    
  23.         public static Delegate CreateDelegateFromMethodInfoByDelegate<T>(Object instance, MethodInfo method)    
  24.         {    
  25.     
  26.             Delegate del = Delegate.CreateDelegate(typeof(T), instance, method);    
  27.             EventHandler mymethod = del as EventHandler;    
  28.             return mymethod;    
  29.         }

  


</>code

  1. private void button3_Click(object sender, EventArgs e)  
  2.         {  
  3.             var Instance = Activator.CreateInstance(typeof(Form1)) as Form1;  
  4.             MethodInfo methodInfo = typeof(Form1).GetMethod("Method1");   
  5.   
  6.              EventHandler dd = Delegate.CreateDelegate(typeof(EventHandler), Instance,methodInfo) as EventHandler;  
  7.             dd(null, null);  
  8.         }  
  9. public void Method1(object sender, EventArgs e)  
  10.         {  
  11.             MessageBox.Show("ee");  
  12.         }


如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛

发表评论 (1503人查看0条评论)
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
昵称:
最新评论
------分隔线----------------------------

快速入口

· 365软件
· 杰创官网
· 建站工具
· 网站大全

其它栏目

· 建站教程
· 365学习

业务咨询

· 技术支持
· 服务时间:9:00-18:00
365建站网二维码

Powered by 365建站网 RSS地图 HTML地图

copyright © 2013-2024 版权所有 鄂ICP备17013400号