gpt4 book ai didi

c# - 阐明 C# 扩展方法的优先级

转载 作者:行者123 更新时间:2023-11-30 21:54:35 27 4
gpt4 key购买 nike

我试图通过创建我自己的同名扩展方法来修改现有扩展方法的行为。我知道只要方法签名不同,这是可能的。我还知道调用哪个方法取决于签名的关闭程度。

如果我有

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com