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

Code
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
}
90
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器