IEnumerable< T>的通用方法的重载 [英] Overload of generic method for IEnumerable<T>?
问题描述
我想泛型方法有两个重载:
public ReturnType< T>DoStuff< T>(T事物){...}公共ReturnType< T>DoStuff< T>(IEnumerable< T>事物){...}
当然,麻烦的是 IEnumerable< T>
本身就是与第一个签名匹配的类型,因此,当我尝试将集合传递给此方法时,它将调用第一个重载./p>
很显然,我可以用不同的方式命名方法以消除歧义.但是,鉴于这些方法本质上是做同样的事情,我希望将它们保留为重载.
在第一个签名中是否有某种定义 T
的方法,以使其不接受 IEnumerable
作为参数?
这是一个hack,但是您可以滥用以下事实:仅当在非扩展方法中未找到匹配项时才考虑扩展方法.
class MyClass {公共ReturnType< T>DoStuff< T>(IEnumerable< T>事物){...}公共ReturnType< T>DoStuffSingle< T>(T事物){...}}静态类MyClassExtensions {公共静态ReturnType< T>DoStuff< T>(这个MyClass myClass,T东西)=>myClass.DoStuffSingle(事物);}
此后,给一个 MyClass myClass;
:
-
myClass.DoStuff(123);
调用采用int
的扩展方法 -
myClass(new [] {123});
调用采用IEnumerable< int>
的实例方法 -
myClass("123");
调用采用IEnumerable< char>
的实例方法 -
myClass(t);
,其中t
是不受约束的通用参数类型T
,调用采用T的扩展方法
,无论T
实现哪种接口.
在我看来,后两个表示您可能不应该沿着这条道路前进,但是没有什么可以阻止您不同意并继续这样做.
I want to have two overloads of a generic method:
public ReturnType<T> DoStuff<T>(T thing) {...}
public ReturnType<T> DoStuff<T>(IEnumerable<T> things) {...}
Trouble is, of course, that an IEnumerable<T>
is itself a type that matches the first signature, so when I try passing a collection to this method, it invokes the first overload.
Obviously I could name the methods differently to remove the ambiguity. But seeing as the methods essentially do the same thing, I'd like to keep them as overloads.
Is there some way of defining T
in the first signature so that it will not accept an IEnumerable
as an argument?
It's a hack, but you can misuse the fact that extension methods are only considered when no match has been found in non-extension methods.
class MyClass {
public ReturnType<T> DoStuff<T>(IEnumerable<T> things) { ... }
public ReturnType<T> DoStuffSingle<T>(T thing) { ... }
}
static class MyClassExtensions {
public static ReturnType<T> DoStuff<T>(this MyClass myClass, T thing)
=> myClass.DoStuffSingle(thing);
}
After this, given a MyClass myClass;
:
myClass.DoStuff(123);
calls the extension method takingint
myClass(new[] {123});
calls the instance method takingIEnumerable<int>
myClass("123");
calls the instance method takingIEnumerable<char>
myClass(t);
, wheret
is of an unconstrained generic parameter typeT
, calls the extension method takingT
, regardless of which interfacesT
implements.
Those last two are an indication that you probably shouldn't continue down this path, in my opinion, but there's nothing stopping you from disagreeing and going with this anyway.
这篇关于IEnumerable< T>的通用方法的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!