黑马程序员技术交流社区

标题: 反射 [打印本页]

作者: brucel50    时间: 2013-9-7 11:27
标题: 反射
通过反射如何调用类中的私有的、泛型的、多参数的方法?
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Reflection;

  6. namespace TypeOf_与_GetType
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Type t1 = typeof(Person);
  13.             MethodInfo mi3 = t1.GetMethod("SayHelloG<T>", BindingFlags.NonPublic | BindingFlags.Instance);
  14.             object obj = Activator.CreateInstance(t1);
  15.             //...
  16.             Console.ReadKey();
  17.             
  18.         }
  19.     }
  20.     class Person
  21.     {
  22.         private void SayHello()
  23.         {
  24.             Console.WriteLine("Hello!");
  25.         }

  26.         private void SayHelloG<T>(T t)
  27.         {
  28.             Console.WriteLine(t.ToString());
  29.         }
  30.     }
  31. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2