反射时怎么得到重载的泛型方法

Imcx 2017-01-13 10:48:51
加载的是Newtonsoft.Json.Net20.dll程序集,只是个举例用的例子,不讨论为什么不引用使用和源码使用

下面是通过Assembly.GetType("Newtonsoft.Json.JsonConvert")得到的一部分


我用的Type.GetMethod(methodName, types)查找方法,这个可以区分普通的重载方法,得到一个确定的方法
比如绿色框里的SerializeObject方法

现在红框里我划箭头的两个如果直接用上面的方法去寻找就会报错

//当绑定到成员的操作导致一个以上的成员匹配绑定条件时引发的异常。
AmbiguousMatchException
...全文
388 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
stherix 2017-01-13
  • 打赏
  • 举报
回复
引用 5 楼 Imcx_ 的回复:
[quote=引用 2 楼 stherix 的回复:] public class Example { public static void Generic<T>(T toDisplay) { Console.WriteLine("\r\nHere it is: {0}", toDisplay); } } Type ex = typeof(Example); MethodInfo mi = ex.GetMethod("Generic"); MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
这个例子不全面,只表现出了怎么反射使用泛型方法[/quote] 你在mi后面打个点号不就知道了 IsGenericMethod()
Imcx 2017-01-13
  • 打赏
  • 举报
回复
引用 2 楼 stherix 的回复:
public class Example { public static void Generic<T>(T toDisplay) { Console.WriteLine("\r\nHere it is: {0}", toDisplay); } } Type ex = typeof(Example); MethodInfo mi = ex.GetMethod("Generic"); MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
这个例子不全面,只表现出了怎么反射使用泛型方法
Imcx 2017-01-13
  • 打赏
  • 举报
回复
引用 3 楼 shingoscar 的回复:
一般都是用GetMethods,然后看IsGeneric、或分析GenericParameters
这个方法我试过,完全是可以实现的
Poopaye 2017-01-13
  • 打赏
  • 举报
回复
一般都是用GetMethods,然后看IsGeneric、或分析GenericParameters
stherix 2017-01-13
  • 打赏
  • 举报
回复
public class Example { public static void Generic<T>(T toDisplay) { Console.WriteLine("\r\nHere it is: {0}", toDisplay); } } Type ex = typeof(Example); MethodInfo mi = ex.GetMethod("Generic"); MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));
shoppo0505 2017-01-13
  • 打赏
  • 举报
回复
没源代码,分析不出来。
Imcx 2017-01-13
  • 打赏
  • 举报
回复
只能是自己去遍历查找吗? 有没有别的查找方法?

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧