- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图通过创建我自己的同名扩展方法来修改现有扩展方法的行为。我知道只要方法签名不同,这是可能的。我还知道调用哪个方法取决于签名的关闭程度。
如果我有
public void DoStuff(this List<string> list) {
//...
}
和
public void DoStuff<T>(this List<T> list) {
//...
}
我知道 DoStuff
将被要求 new List<string>().DoStuff()
, 而 DoStuff<T>
将被要求 new List<int>().DoStuff()
.
我不明白的是为什么我在使用界面时没有看到相同的行为。
public interface IBar { }
public class Foo : IBar
{
}
public static class FooHelper
{
public static void DoStuff(this IBar stuff)
{
Console.WriteLine("public static void DoStuff (this IBar stuff)");
}
public static void DoStuff<T>(this T stuff)
{
Console.WriteLine("public static void DoStuff<T>(this T stuff)");
}
}
class Program
{
static void Main(string[] args)
{
var foo = new Foo();
foo.DoStuff();
}
}
在上面的例子中,我期望 public static void DoStuff (this IBar stuff)
到输出,但是我得到 public static void DoStuff<T>(this T stuff)
.我假设接口(interface)类型比通用类型更具体,因此是我的 DoStuff
的更好选择。打电话。
为什么选择泛型类型签名而不是接口(interface)类型签名?
最佳答案
Chris 的评论是正确的,以下是 C# 语言规范中的更多详细信息:
调用扩展方法foo.DoStuff()
等同于调用静态方法FooHelper.DoStuff(foo)
。
编译器必须确定要调用的确切方法(§7.6.5.1 方法调用)。首先,它将创建一组候选方法。两个 DoStuff 方法都会在这个集合中,因为
o If F is non-generic, F is a candidate when:
• M has no type argument list, and
• F is applicable with respect to A (§7.5.3.1).
o If F is generic and M has no type argument list, F is a candidate when:
• Type inference (§7.5.2) succeeds, inferring a list of type arguments for the call, and
• Once the inferred type arguments are substituted for the corresponding method type parameters, all constructed types in the parameter list of F satisfy their constraints (§4.4.4), and the parameter list of F is applicable with respect to A (§7.5.3.1).
通用 FooHelper.DoStuff
方法将已经推断出类型参数(§7.5.2):
Type inference occurs as part of the binding-time processing of a method invocation (§7.6.5.1) and takes place before the overload resolution step of the invocation.
If type inference succeeds, then the inferred type arguments are used to determine the types of arguments for subsequent overload resolution.
所以候选集是
public static void DoStuff(this IBar stuff)
public static void DoStuff<Foo>(this Foo stuff)
编译器将从这个集合中选择最好的方法来调用。规则在 §7.5.3.2 中描述。在这种情况下,将调用泛型方法,因为身份转换(从 Foo 到 Foo)比从 Foo 到 IBar 的转换要好。
关于c# - 阐明 C# 扩展方法的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32947238/
我声明了一个通用结构和这些结构的数组,如下所示: struct A { int x,y,z; char a,b,c; }; struct A *str_arr[5]; 根据我的理解,s
我对 CUDA 编程指南 4.0 部分 5.3.2.1 中的以下语句感到困惑 在性能指南的章节中。 Global memory resides in device memory and device
我想知道这两者之间有什么区别: addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.11.3" cross CrossVersion.
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
结构体的析构函数: 您能否指定必须在结构的析构函数中显式处理的每种数据类型? struct Node { int val; // representing any
我试图通过创建我自己的同名扩展方法来修改现有扩展方法的行为。我知道只要方法签名不同,这是可能的。我还知道调用哪个方法取决于签名的关闭程度。 如果我有 public void DoStuff(this
取自:https://www.gnu.org/software/libc/manual/html_node/Nonreentrancy.html For example, suppose that t
谁能帮忙解释一下 bind 在这种情况下的用途是什么? Add New https://youtu.be/OKRu7i49X54?list=PL6gx4Cwl9DGBuKtLgPR_zWYnrwv-J
目前我将 glFrustum 设置为 glFrustum(0, 100.0, 0, 100.0, -50, 50); 我的图像是一个矩形立方体,尺寸为:当形状的尺寸小于查看框时,为什么我看不到图像?
我面临着与该线程中描述的类似的问题: Can Enunciate generate docs for an API that handles generic types? 我正在使用 enunciat
有人能解释一下这段代码是如何工作的吗? PRE_PROC_EXE := $(shell which pre_proc.pl) PRE_PROC2_EXE := $(shell
使用 Prompter 方法在 FormFlow 中发送自定义卡片。查看代码发现有一个 GenerateMessages() 方法,对于下面的代码,它总是返回 false。有人可以阐明为什么/何时使用
我在谷歌上找不到任何东西。 我有这段代码: Random r = new Random(); int[] output = Enumerable.Range(0, 11).Select(x => x
我正在阅读 Eric Meyer 撰写的 CSS 权威指南第 3 版。 他关于字体大小的部分(第 107 页)指出字体大小决定了 em 框。如果我这样做: span { font-size: 10px
我对ElasticSearch的使用有些困惑。我现在有一个带有关系数据库(Ruby on Rails)的应用程序(Mysql),我正试图从ElasticSearch提供的搜索功能中受益。我仍然想像以前
有Java背景,但是从未开发过 Web 客户端(即由浏览器运行的脚本),但是java小程序(我怀疑是,嗯,老式的(对吧?)或者其他我显然会将它们用于以下一些用途) 然后考虑以下场景: 仅客户端应用程序
假设我有一个子函数: function Child() {} 并且有一个父函数: function Parent() {} 然后我将 Child 的原型(prototype)设置为 Parent 的新
程序如下: #include using namespace std; class X { int no; public: X(int n=1) : no{n} {} X(const
阅读文档: del: Deletion of a target list recursively deletes each target, from left to right. 你能解释一下为什么这
我对 C/C++ 中的悬挂指针有点困惑 void remove(){ Node* curr = new Node(10); Node* pt = curr; delete curr; // do so
我是一名优秀的程序员,十分优秀!