引用所需的重载泛型方法
给定的
public Class Example
{
public static void Foo< T>(int ID){}
public static void Foo< T,U>(int ID){}
}
问题:
- 将其称为“重载通用方法”是否正确?
如何在创建 MethodInfo 对象时指定任一方法?
类型 exampleType = Type.GetType("完全限定的nameOfExample,namespaceOfExample"); MethodInfo mi = exampleType.GetMethod("Foo", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof(Type), typeof(Type) }, null);
参数 4 导致编译器非常不满
given
public Class Example
{
public static void Foo< T>(int ID){}
public static void Foo< T,U>(int ID){}
}
Questions:
- Is it correct to call this an "overloaded generic method"?
How can either method be specified in creation of a MethodInfo object?
Type exampleType = Type.GetType("fullyqualifiednameOfExample, namespaceOfExample"); MethodInfo mi = exampleType.GetMethod("Foo", BindingFlags.Public|BindingFlags.Static, null, new Type[] {typeof(Type), typeof(Type) }, null);
argument 4 causes the compiler much displeasure
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我找不到一种使用 GetMethod 来完成您想要的操作的方法。 但您可以获取所有方法并浏览列表,直到找到所需的方法。
请记住,您需要先调用 MakeGenericMethod,然后才能实际使用它。
I can't find a way of using GetMethod that would do what you want. But you can get all the methods and go through the list until you find the method that you want.
Remember you need to call MakeGenericMethod before you can actually use it.
以下是您的问题的答案以及示例:
是的,尽管这里对于泛型方法有两件事确实需要注意:类型推断和重载方法解析。 类型推断发生在编译时,编译器尝试解析重载方法签名之前。 编译器将类型推断逻辑应用于共享相同名称的所有泛型方法。 在重载解析步骤中,编译器仅包含那些类型推断成功的泛型方法。 更多信息...
请参阅下面的完整示例控制台应用程序代码显示了如何在创建 MethodInfo 对象时指定 Foo 方法的多个变体,然后使用扩展方法调用:
Program.cs
Example.cs:
Extensions.cs:
Here are the answers to your questions along with an example:
Yes, although there are two things really to be aware of here with generic methods, type inference and overload method resolution. Type inference occurs at compile time before the compiler tries to resolve overloaded method signatures. The compiler applies type inference logic to all generic methods that share the same name. In the overload resolution step, the compiler includes only those generic methods on which type inference succeeded. More here...
Please see the full example Console Application program code below that shows how several variants of the Foo method can be specified in creation of a MethodInfo object and then invoked using an Extension method:
Program.cs
Example.cs:
Extensions.cs:
更好:
尝试获得
System.Linq.Enumerable.Select
的正确重载的示例Better:
Example attempt to get the correct overload of
System.Linq.Enumerable.Select
另一种查找所需
MethodInfo
的方法是创建一个委托;Another one liner to find the
MethodInfo
you want is to create a delegate;这是满足您需要的 Linq 一行:
Here is a Linq one-liner for what you need:
我对您的 lambda 查询做了一些修改。
当参数类型为通用时,您必须这样做:
我添加
pi.ParameterType.GetGenericTypeDefinition()
,这样
该方法工作得非常好
示例:
通过修改我可以调用的方法
这个扩展方法
用我的新助手这样
我的助手的签名是
I make a little modification of your lambda query.
When the parameter type si generic you must make that like that :
I add
pi.ParameterType.GetGenericTypeDefinition()
and
In this way the method working very fine
Example :
With the modification of the method i can call
this extension method
With my new Helper like this
The signature of my helper is