一种获取重载泛型方法的方式

今天看到hbb0b0的一个帖子:如何用反射实现如下的泛型方法调用? , 询问如何获取一个重载的泛型方法。
因为Type没有提供GetGenericMethod方法,调用GetMethod可能会抛出一个AmbiguousMatchException异常或者无法获得正确的泛型方法。
本文提供一种途径,通过查询Type所有的Method找到正确的方法。

1 using  System;
 2 using  System.Collections.Generic;
 3 using  System.Linq;
 4 using  System.Text;
 5 using  System.Reflection;
 6
 7 namespace  Com.Colobu.Demo
 8 {
 9      class  Program
10      {
11          static   void  Main( string [] args)
12          {
13             Demo < string >  demo  =   new  Demo < string > ();
14             InvokeMethods(demo);
15             Console.WriteLine( " ================================== " );
16             InvokeMethodsByReflect(demo);
17
18             Console.Read();
19         }

20
21          static   void  InvokeMethods(Demo < string >  demo)
22          {
23             demo.MethodA( 1 );
24             demo.MethodA();
25             demo.MethodA( " hello " );
26             demo.MethodA < int > ( 4 );
27             demo.MethodA < int long > ( 5 );
28         }

29
30
31          static   void  InvokeMethodsByReflect(Demo < string >  demo)
32          {
33             Type demoType  =  demo.GetType();
34
35              // the below throw an AmbiguousMatchException
36              // MethodInfo mi = demoType.GetMethod("MethodA");
37
38             MethodInfo mi  =  demoType.GetMethod( " MethodA " new  Type[]  typeof ( int ) } );  // get the 1st method
39             mi.Invoke(demo,  new   object [] { 1 } );
40
41             mi  =  demoType.GetMethod( " MethodA " , new  Type[] {} );  // get the 2nd method
42             mi.Invoke(demo,  null );
43             
44             mi  =  demoType.GetMethod( " MethodA " new  Type[]  typeof ( string ) } );  // get the 3rd method
45             mi.Invoke(demo,  new   object []  " hello "  } );
46
47             mi  =  demoType.GetMethods().First(m  =>  m.Name.Equals( " MethodA " &&  m.IsGenericMethod  &&  m.GetGenericArguments().Length  ==   1 );
48             mi.MakeGenericMethod( typeof ( int )).Invoke(demo,  new   object []  4  } );
49
50             mi  =  demoType.GetMethods().First(m  =>  m.Name.Equals( " MethodA " &&  m.IsGenericMethod  &&  m.GetGenericArguments().Length  ==   2 );
51             mi.MakeGenericMethod( typeof ( int ), typeof ( long )).Invoke(demo,  new   object []  5  } );
52
53
54         }

55
56        
57     }

58
59      class  Demo < U >
60      {
61          public    void  MethodA( int  arg)
62          {
63             Console.WriteLine( " 1: "   +  arg.ToString());
64         }

65
66          public    void  MethodA()
67          {
68
69             Console.WriteLine( " 2:null " );
70         }

71
72          public   void  MethodA(U arg)
73          {
74             Console.WriteLine( " 3: "   +  arg.ToString());
75         }

76
77          public   void  MethodA < T > (T arg)
78          {
79             Console.WriteLine( " 4: "   +  arg.ToString());
80         }

81
82          public   void  MethodA < T,S > (T arg)
83          {
84             Console.WriteLine( " 5: "   +  arg.ToString());
85         }

86         
87     }

88
89 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值