简体   繁体   English

区分重载的通用方法和非通用方法

[英]Distinguishing Between Overloaded Generic and Non-Generic Methods

This must be covered somewhere but I'm having difficulty expressing my search criteria, so... 必须在某处进行覆盖,但是我很难表达我的搜索条件,所以...

Below are three instance methods declared by the same type. 下面是用相同类型声明的三个实例方法。

void Invoke(int timeout);
void Invoke<T>(T data);
T Invoke<T>(int timeout);

I want to invoke the second method, passing an Int32 argument and without using reflection. 我想调用第二个方法,传递一个Int32参数并且不使用反射。 Options include: 选项包括:

Invoke(1);
Invoke<int>(1);
Invoke((int)1);
Invoke<int>((int)1);

However, none of these calls the desired method. 但是,这些都没有调用所需的方法。 Can I achieve what I want or should I resort to method-renaming? 我可以实现我想要的结果还是应该使用方法重命名?

Note that if the third method did not exist, I could simply do this: 请注意,如果不存在第三个方法,我可以简单地做到这一点:

Invoke(1); // Invokes the first method
Invoke<int>(1); // Invokes the second method

使用命名参数:

Invoke(data: 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM