开发者

Why is generated override code for generic method incorrect?

开发者 https://www.devze.com 2023-01-11 18:31 出处:网络
I have an abstract generic class public abstract class Foo<TType> with an abstract method public abstract object DoSomething(TType arg = default(TType)) {}

I have an abstract generic class

public abstract class Foo<TType>

with an abstract method

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

Now, the inherited class

public class BabyFoo : Foo<string>

when I want to override DoSomething and start typing "override " to get the intell开发者_如何学Goisense/generator to write a method skeleton I expected

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

or even

public override object DoSomething(string arg = null)

but it literally comes up with

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

My initial thought is it is a VS2010 bug since optional params are new to c#, but can anybody tell me if there is perhaps a real reason why (reference types vs value types??) the IDE generates this code?


Just to clarify:

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));
}

Unless there's something I'm missing, it's quite simply a bug in the Visual Studio IDE / code-gen. Changing the method signature to the "expected" one results in code that will compile, as the "actual" one refuses to compile thanks to being clearly invalid.

Having tried a few different types for TType as well as things like the where TType : new() constraint, I couldn't get VS to generate valid code with your DoSomething method.

Congratulations - you've (probably) found a bug in Visual Studio =)

There are always edge cases when it comes to code generation, I logged one for Visual Basic 2005/2008 a long time ago that was resolved WONT FIX as it was a really obscure one comparatively. Hopefully this one'll be fixed though!

0

精彩评论

暂无评论...
验证码 换一张
取 消