黑马程序员技术交流社区
标题:
关于非泛型类中有重载的私有泛型方法的反射问题
[打印本页]
作者:
brucel50
时间:
2013-9-9 14:31
标题:
关于非泛型类中有重载的私有泛型方法的反射问题
<P> </P>
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace _9反射练习题
{
class Program
{
static void Main(string[] args)
{ //小弟送上代码一枚,请多多指教:
Type Typ1 = typeof(MyGenericClass1);
MethodInfo[] miArray1 = Typ1.GetMethods();
Type[] MethodArgumenttypes = null;
ParameterInfo[] piArray1 = null;
MethodInfo miM1 = miArray1.First(m => m.Name == "M1"
&& m.IsGenericMethod
&& (MethodArgumenttypes = m.GetGenericArguments()).Length == 1
&& MethodArgumenttypes[0].Name == "T1"
&& (piArray1 = m.GetParameters()).Length == 2
&& piArray1[0].ParameterType == MethodArgumenttypes[0]
&& piArray1[1].ParameterType == typeof(String));
MethodInfo miM2 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
&& m.IsGenericMethod
&& (MethodArgumenttypes = m.GetGenericArguments()).Length == 2
&& MethodArgumenttypes[0].Name == "T1"
&& MethodArgumenttypes[1].Name == "T2"
&& (piArray1 = m.GetParameters()).Length == 3
&& piArray1[0].ParameterType == MethodArgumenttypes[0]
&& piArray1[1].ParameterType == typeof(String)
&& piArray1[2].ParameterType == MethodArgumenttypes[1]);
MethodInfo miM3 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
//private void M1<T1>(T1 t1)
&& (MethodArgumenttypes = m.GetGenericArguments()).Length == 1
&& MethodArgumenttypes[0].Name == "T1"
&& (piArray1 = m.GetParameters()).Length == 1
&& piArray1[0].ParameterType == MethodArgumenttypes[0]);
MethodInfo miM4 = Typ1.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance).First(m => m.Name == "M1"
&& (MethodArgumenttypes = m.GetGenericArguments()).Length == 2
&& MethodArgumenttypes[0].Name == "T1"
&& (piArray1 = m.GetParameters()).Length == 3
&& piArray1[0].ParameterType == MethodArgumenttypes[0]
&& piArray1[1].ParameterType == MethodArgumenttypes[1]
&& piArray1[2].ParameterType == typeof(String));
//private void M1<T1, T2>(T1 t, T2 t2, String s)
object obj1 = Activator.CreateInstance(Typ1);
miM1 = miM1.MakeGenericMethod(new Type[] { typeof(Int32) });
miM2 = miM2.MakeGenericMethod(new Type[] { typeof(Int32),typeof(Int32)});
miM3 = miM3.MakeGenericMethod(new Type[] { typeof(String)});
miM4 = miM4.MakeGenericMethod(new Type[] { typeof(Int32),typeof(Int32)});
miM1.Invoke(obj1, new object[] { 12, "哈哈" });
miM2.Invoke(obj1, new object[] { 12,"哈哈哈",12});
miM3.Invoke(obj1, new object[] { "我是第三个方法"});
miM4.Invoke(obj1, new object[] { 12,12,"完成!"});
Console.ReadKey();
}
}
class MyGenericClass1
{
public void M1<T1>(T1 t11, String TC1)
{
Console.WriteLine("*************************");
Console.WriteLine("我是方法M1");
Console.WriteLine(t11.ToString() + TC1);
Console.WriteLine("*************************");
}
private void M1<T1, T2>(T1 t11, String TC1, T2 t21)
{
Console.WriteLine("*************************");
Console.WriteLine("我也是方法M1");
Console.WriteLine(t11.ToString() + TC1.ToString() + t21.ToString());
Console.WriteLine("*************************");
}
private void M1<T1>(T1 t1)
{
Console.WriteLine("*************************");
Console.WriteLine("我还是方法M1");
Console.WriteLine(t1.ToString());
Console.WriteLine("*************************");
}
private void M1<T1, T2>(T1 t, T2 t2, String s)
{
Console.WriteLine("*************************");
Console.WriteLine("我就是方法M1");
Console.WriteLine(t.ToString() + t2.ToString() + s);
Console.WriteLine("*************************");
}
}
}
复制代码
作者:
杨靖
时间:
2013-9-10 08:39
进来看看,学习下
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2