如何获取泛型方法的MethodInfo? [英] How to get MethodInfo of a generic method?

查看:45
本文介绍了如何获取泛型方法的MethodInfo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取该方法的 MethodInfo 对象:

I am trying to get a MethodInfo object for the method:

Any<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>)

我遇到的问题是如何为 Func< TSource,Boolean> 位指定类型参数...

The problem I'm having is working out how you specify the type parameter for the Func<TSource, Boolean> bit...

MethodInfo method = typeof(Enumerable).GetMethod("Any", new[] { typeof(Func<what goes here?, Boolean>) });

帮助表示赞赏.

推荐答案

无法在单个调用中获取它,因为您需要创建一个由该方法的泛型参数构造的泛型类型(在这种情况下为TSource).并且由于它是特定于该方法的,因此您需要获取该方法以获取它并构建泛型Func类型.鸡肉和鸡蛋有问题吗?

There's no way of getting it in a single call, as you would need to make a generic type constructed of the generic parameter of the method (TSource in this case). And as it's specific to the method, you would need to get the method to get it and build the generic Func type. Chicken and egg issue heh?

您可以做的是获取在Enumerable上定义的所有Any方法,并遍历这些方法以获得所需的方法.

What you can do though is to get all the Any methods defined on Enumerable, and iterate over those to get the one you want.

这篇关于如何获取泛型方法的MethodInfo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆