您如何为采用通用参数(不使用GetMethods)的通用函数调用GetMethod? [英] How do you call GetMethod for a generic function that takes a generic parameter (without using GetMethods)?

查看:35
本文介绍了您如何为采用通用参数(不使用GetMethods)的通用函数调用GetMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 GetMethods 获取方法信息,但是我想知道如何在没有 GetMethods 的情况下正确地进行操作.我读过其他SO问题和答案,表明这是不可能的,或者建议仅使用LINQ,但这并不是对问题的真正答案.

I know I can fetch the method info using GetMethods, but I want to know how to do it properly without GetMethods. I have read other SO questions and answers that suggest this is not possible, or suggest just using LINQ instead, but that isn't really an answer to the question.

在最基本的级别上考虑一个采用单个通用参数的静态通用函数.

Consider at the most basic level, a static generic function that takes a single generic parameter.

private static void Test<T>(T val)
{
}

要获取此方法信息,我们可以调用 Type.GetMethod("Test",BindingFlags.Static | BindingFlags.NonPublic).但是,如果由于某些原因我们无法使用此简单的 GetMethod 签名(可能由于多次重载),则需要提供参数类型.问题是我无法创建与 T val 参数精确匹配的参数类型.有趣的是,我可以从方法信息中获取参数(通过 GetMethods 获取),然后将其传递到 GetMethod 中以获得所需的结果.这意味着,如果只能创建适当的泛型类型(将 IsGenericParameter 设置为true),那么我觉得这将是完全可能的.

To fetch this method info we can just call Type.GetMethod("Test", BindingFlags.Static | BindingFlags.NonPublic). However if there were some reason we could not use this simple GetMethod signature (perhaps due to multiple overloads), then we need to supply the parameter types. The problem is that I cannot create a parameter type that accurately matches the T val parameter. What's interesting is that I can take the parameters from the method info (fetched with GetMethods) and pass that into GetMethod to get the desired outcome. This means that if it were only possible to create the appropriate generic types (with IsGenericParameter set to true) then I feel like this would be completely possible.

这意味着这在.NET中是完全可能的,并且只需要创建适当的类型实例即可.一个如何创建这些类型实例?如果无法创建它们,为什么不呢?

So that means that this is entirely possible in .NET, and only require the creation of the proper type instances. How does one create these type instances? And if they are not possible to create, why aren't they?

我创建了一个简单的小提琴来展示问题.

推荐答案

它不容易使用,因为您需要的类型实际上是方法/参数定义中仅存在的通用类型参数.例如,在您的 Test< T>(T val)中,参数类型是" Test< T> 所定义的 T .您不能构造,因为它不是由组成的,获取 T 的唯一方法是通过 GetParameters().

It isn't readily available, because the types you need are actually generic type arguments that only exist in the method / parameter definition. For example, in your Test<T>(T val), the parameter type is "the T as defined by Test<T>. You can't construct that, because it isn't composed from anything. The only way to obtain that T is via GetParameters().

基本上,这留下了:困难的方式-即手动操作.例如:

Basically, that leaves: the hard way - i.e. manually. For example:

var method1 = typeof(Program).GetMethods(flags).Single(x => x.Name == "Test"
     && x.IsGenericMethodDefinition && x.GetParameters().Length == 1
      && x.GetParameters()[0].ParameterType == x.GetGenericArguments()[0])
    .MakeGenericMethod(paramTypes1);

很显然,如果您知道只有一个 Test(...)方法,这会更简单:

Obviously it is simpler if you know there is only one Test(...) method:

var method = typeof(Program).GetMethod("Test", flags)
    .MakeGenericMethod(paramTypes1);

这篇关于您如何为采用通用参数(不使用GetMethods)的通用函数调用GetMethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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