IEnumerable< T>的通用方法的重载

编程入门 行业动态 更新时间:2024-10-12 10:18: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;的通用方法的重载

本文发布于:2023-11-07 19:27:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567218.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   amp   IEnumerable   gt   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!