gpt4 book ai didi

c# - 为什么 C# 不能正确绑定(bind)泛型覆盖方法?

转载 作者:太空狗 更新时间:2023-10-29 18:25:50 27 4
gpt4 key购买 nike

我定义了以下类和方法:

 using System;
using System.Linq.Expressions;
using System.Windows.Forms;

public class ReturnValue<T, S> {}

public class Something<T>
{
// Sorry about the odd formatting. Trying to get it to fit nicely...
public ReturnValue<T, C>
Do<C, S>(C control, Expression<Func<C, S>> controlProperty)
where C : Control
{
return new ReturnValue<T, C>();
}

public ReturnValue<T, ToolStripItem>
Do<S>(ToolStripItem control, Expression<Func<ToolStripItem, S>> controlProperty)
{
return new ReturnValue<T, ToolStripItem>();
}
}

这编译得很好。呜呜!半路那里。然后,我稍后尝试将其与如下代码一起使用:

 var toolStripItem = new ToolStripStatusLabel();

var something = new Something<string>();
something.Do(toolStripItem, t => t.Text); // Does not compile

然而,这会因以下错误消息而终止

The type ToolStripStatusLabel cannot be used as type parameter C in the generic type or method Something<T>.Do<C,S>(C, Expression<Func<C,S>>). There is no implicit reference conversion from ToolStripStatusLabel to Control.

在我看来,C# 编译器在这种情况下失败了,尽管这两个方法没有创建一组不明确的方法声明。 ControlToolStripStatusLabelComponent 的继承树中作为 sibling 存在.我认为编译器会有足够的信息来正确绑定(bind)客户端代码中的方法调用。

但是,如果我对自己的兄弟类做同样的事情,那么一切都可以正常编译。

 public class Parent {}
public class Child1 : Parent {}
public class Child2 : Parent {}

public class Something2<T>
{
public ReturnValue<T, C>
Do<C, S>(C control, Expression<Func<C, S>> controlProperty)
where C : Child1
{
return new ReturnValue<T, C>();
}

public ReturnValue<T, Child2>
Do<S>(Child2 control, Expression<Func<Child2, S>> controlProperty)
{
return new ReturnValue<T, Child2>();
}
}

var child2 = new Child2();
var something2 = new Something2<string>();
something2.Do(child2, c => c.GetType()); // Compiles just fine

任何人都可以阐明我做错了什么,如果有的话?

最佳答案

问题是第一个方法在重载决策的候选集中,因为类型约束C : Control 只在之后 已执行过载决议。我相信您希望它早日被淘汰 - 但事实并非如此。

现在,如果您处理 C = ToolStripItem,第一个重载比第二个更具体 - 因此重载解析的结果是选择第一个版本。

然后应用了类型约束验证……但失败了。

我有一个 blog post on this matter这可能会帮助您了解该过程,然后another blog post我以一种相当愚蠢的方式应用规则。

编辑:在您的第二个示例中,参数的类型完全是第一个参数中指定的类型,因此第一个方法最终不会更具体。第二种方法由于类型参数较少(我认为;我没有详细检查)而获胜,然后经过验证并通过。

用 ToolStripItem 术语来说,您实际上可以通过一个简单的更改来编译您的第一个示例:

// Change this
var toolStripItem = new ToolStripStatusLabel();
// To this...
ToolStripItem toolStripItem = new ToolStripStatusLabel();

toolStripItem 的编译时类型从 ToolStripStatusLabel 更改为 ToolStripItem 消除了第一种方法的“优势”,因此它然后编译。

关于c# - 为什么 C# 不能正确绑定(bind)泛型覆盖方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4628191/

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