如何通过反射调用普通类中的泛型方法?(有重载)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace 复习
{
class Program
{
static void Main(string[] args)
{
Type t1 = typeof(Person);
MethodInfo mi1 = t1.GetMethod("SayHello", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { }, null);
MethodInfo mi2 = t1.GetMethod("SayHello", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(String) }, null);
MethodInfo mi3 = t1.GetMethod("SayHello", BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(String), typeof(Int32) }, null);
//如何分别反射以下三个方法?
//private void SayHello<T1, T2, T3>(T1 t1, T2 t2, T3 t3)
//private void SayHello<T1>(T1 t)
//private void GM1<T1>(T1 t)
object obj1 = Activator.CreateInstance(t1);
//开始调用
//
Console.ReadKey();
}
}
class Person
{
private void SayHello()
{
Console.WriteLine("我是一个无参的方法");
}
private void SayHello(string s)
{
Console.WriteLine("我是一个有参的方法,参数:{0}", s);
}
private void SayHello(string s, int i)
{
Console.WriteLine("我是一个有两个参数的方法,参数:{0}、{1}", s, i);
}
private void SayHello<T1, T2, T3>(T1 t1, T2 t2, T3 t3)
{
Console.WriteLine(t1.ToString() + t2.ToString() + t3.ToString());
}
private void SayHello<T1>(T1 t)
{
Console.WriteLine(t.ToString());
}
private void GM1<T1>(T1 t)
{
Console.WriteLine(t.ToString());
}
}
}
...全文


