为什么生成的泛型方法的覆盖代码不正确?

时间:2010-08-17 14:42:40

标签: visual-studio-2010 optional-parameters

我有一个抽象的泛型类

public abstract class Foo<TType>

使用抽象方法

public abstract object DoSomething(TType arg = default(TType)) {}

现在,继承的类

public class BabyFoo : Foo<string>

当我想覆盖DoSomething并开始输入“override”以使intellisense / generator编写一个我期望的方法框架

public override object DoSomething(string arg = default(string))

甚至

public override object DoSomething(string arg = null)

但确实提出了

public override object DoSomething(string arg = default(TType))

我最初的想法是它是一个VS2010错误,因为可选参数对于c#来说是新手,但是有人可以告诉我是否有一个真正的原因(引用类型与值类型??)IDE生成此代码?

1 个答案:

答案 0 :(得分:4)

只是为了澄清:

public abstract class Foo<TType>
{
    public abstract object DoSomething(TType arg = default(TType));
}

public class BabyFoo : Foo<string>
{
    // Expected:
    public override object DoSomething(string arg = default(string))
    // Actual:
    public override object DoSomething(string arg = default(TType));
}

除非我缺少某些内容,否则它只是Visual Studio IDE / code-gen中的一个错误。将方法签名更改为“预期”签名会导致编译代码,因为“实际”代码由于明显无效而拒绝编译。

TType尝试了几种不同的类型以及where TType : new()约束之类的内容,我无法让VS使用您的DoSomething方法生成有效代码。

恭喜 - 您(可能)在Visual Studio中发现了一个错误=)

在代码生成方面总是存在边缘情况,很久以前我记录one for Visual Basic 2005/2008已经解决了WONT FIX,因为它比较模糊了真的。希望这个可以修复!