一种获取重载泛型方法的方式
2009-09-28 11:00
330 查看
今天看到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
}
, 询问如何获取一个重载的泛型方法。
因为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
}
相关文章推荐
- 一种获取重载泛型方法的方式
- 获取一个重载的泛型方法
- 一种IP的获取方式
- java script Request获取URL地址栏属性的方法 (Get方式传参)+eval()使用
- 关于Java的泛型在所声明的对象中如何获取class或者实例的方法的总结
- 在c文档中C2143问题出现的一种方式及解决方法
- 用 Win32 API 建立桌面快捷方式的一种方法
- 一直苦恼于找不到获取系统默认的UserAgent的方法,今天偶然想到一种方法,可实现效果
- 如何获取方法参数的泛型类型
- 一种获取无符号整数最大值的方法:
- 以非泛型方式调用泛型方法
- 改进的“以非泛型方式调用泛型方法”之基于DynamicMethod的实现
- .net后台使用post方式对指定地址的方法传值并且获取结果的方法
- java再复习——通过反射获取到方法的参数和返回值的泛型类型
- ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源 解决方法
- js方法获取get方式传过来的参数
- 利用反射,泛型,静态方法快速获取表单值到Model。
- 一种通过注册表获取USB虚拟串口端口号的方法
- 通过反射的方式来执行静态类的泛型方法
- Cocos2dx 3.0 过渡篇(二十三) C++11一种新随机数获取方法