IEnumerable< T>的通用方法的重载 [英] Overload of generic method for IEnumerable<T>?

查看:35
本文介绍了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 taking int
  • myClass(new[] {123}); calls the instance method taking IEnumerable<int>
  • myClass("123"); calls the instance method taking IEnumerable<char>
  • myClass(t);, where t is of an unconstrained generic parameter type T, calls the extension method taking T, regardless of which interfaces T 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&lt; T&gt;的通用方法的重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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