A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始


  1. <P> </P>
复制代码
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;

  6. namespace _9反射练习题
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. { //小弟送上代码一枚,请多多指教:
  12. Type Typ1 = typeof(MyGenericClass1);
  13. MethodInfo[] miArray1 = Typ1.GetMethods();
  14. Type[] MethodArgumenttypes = null;
  15. ParameterInfo[] piArray1 = null;
  16. MethodInfo miM1 = miArray1.First(m => m.Name == "M1"
  17. && m.IsGenericMethod
  18. && (MethodArgumenttypes = m.GetGenericArguments()).Length == 1
  19. && MethodArgumenttypes[0].Name == "T1"
  20. && (piArray1 = m.GetParameters()).Length == 2
  21. && piArray1[0].ParameterType == MethodArgumenttypes[0]
  22. && piArray1[1].ParameterType == typeof(String));
  23. MethodInfo miM2 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
  24. && m.IsGenericMethod
  25. && (MethodArgumenttypes = m.GetGenericArguments()).Length == 2
  26. && MethodArgumenttypes[0].Name == "T1"
  27. && MethodArgumenttypes[1].Name == "T2"
  28. && (piArray1 = m.GetParameters()).Length == 3
  29. && piArray1[0].ParameterType == MethodArgumenttypes[0]
  30. && piArray1[1].ParameterType == typeof(String)
  31. && piArray1[2].ParameterType == MethodArgumenttypes[1]);
  32. MethodInfo miM3 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
  33. //private void M1<T1>(T1 t1)
  34. && (MethodArgumenttypes = m.GetGenericArguments()).Length == 1
  35. && MethodArgumenttypes[0].Name == "T1"
  36. && (piArray1 = m.GetParameters()).Length == 1
  37. && piArray1[0].ParameterType == MethodArgumenttypes[0]);
  38. MethodInfo miM4 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
  39. && (MethodArgumenttypes = m.GetGenericArguments()).Length == 2
  40. && MethodArgumenttypes[0].Name == "T1"
  41. && (piArray1 = m.GetParameters()).Length == 3
  42. && piArray1[0].ParameterType == MethodArgumenttypes[0]
  43. && piArray1[1].ParameterType == MethodArgumenttypes[1]
  44. && piArray1[2].ParameterType == typeof(String));
  45. //private void M1<T1, T2>(T1 t, T2 t2, String s)
  46. object obj1 = Activator.CreateInstance(Typ1);
  47. miM1 = miM1.MakeGenericMethod(new Type[] { typeof(Int32) });
  48. miM2 = miM2.MakeGenericMethod(new Type[] { typeof(Int32),typeof(Int32)});
  49. miM3 = miM3.MakeGenericMethod(new Type[] { typeof(String)});
  50. miM4 = miM4.MakeGenericMethod(new Type[] { typeof(Int32),typeof(Int32)});
  51. miM1.Invoke(obj1, new object[] { 12, "哈哈" });
  52. miM2.Invoke(obj1, new object[] { 12,"哈哈哈",12});
  53. miM3.Invoke(obj1, new object[] { "我是第三个方法"});
  54. miM4.Invoke(obj1, new object[] { 12,12,"完成!"});
  55. Console.ReadKey();
  56. }
  57. }
  58. class MyGenericClass1
  59. {
  60. public void M1<T1>(T1 t11, String TC1)
  61. {
  62. Console.WriteLine("*************************");
  63. Console.WriteLine("我是方法M1");
  64. Console.WriteLine(t11.ToString() + TC1);
  65. Console.WriteLine("*************************");
  66. }
  67. private void M1<T1, T2>(T1 t11, String TC1, T2 t21)
  68. {
  69. Console.WriteLine("*************************");
  70. Console.WriteLine("我也是方法M1");
  71. Console.WriteLine(t11.ToString() + TC1.ToString() + t21.ToString());
  72. Console.WriteLine("*************************");
  73. }
  74. private void M1<T1>(T1 t1)
  75. {
  76. Console.WriteLine("*************************");
  77. Console.WriteLine("我还是方法M1");
  78. Console.WriteLine(t1.ToString());
  79. Console.WriteLine("*************************");
  80. }
  81. private void M1<T1, T2>(T1 t, T2 t2, String s)
  82. {
  83. Console.WriteLine("*************************");
  84. Console.WriteLine("我就是方法M1");
  85. Console.WriteLine(t.ToString() + t2.ToString() + s);
  86. Console.WriteLine("*************************");
  87. }
  88. }
  89. }
复制代码

1 个回复

正序浏览
进来看看,学习下
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马