30

See the following code:

public abstract class Base
{
    public virtual void Foo<T>() where T : class
    {
        Console.WriteLine("base");
    }
}

public class Derived : Base
{
    public override void Foo<T>()
    {
        Console.WriteLine("derived");
    }

    public void Bang()
    {
        Action bang = new Action(delegate { base.Foo<string>(); });
        bang();    //VerificationException is thrown
    }
}

new Derived().Bang(); throws an exception. Inside the generated CIL of the method Bang I got:

call instance void ConsoleApp.Derived::'<>n__FabricatedMethod1'<string>()

and the signature of the compiler generated method:

method private hidebysig 
    instance void '<>n__FabricatedMethod1'<T> () cil managed 
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )       
    .maxstack 8

    IL_0000: ldarg.0
    IL_0001: call instance void ConsoleApp.Base::Foo<!!T>()
    IL_0006: ret
}

I think the correct code should be '<>n__FabricatedMethod1'<class T>. Is it a bug? By the way, without using delegate{ }(lambda expression is the same), the code works fine with syntax sugars.

Action good = new Action(base.Foo<string>());
good();  //fine

EDIT I'm using VS2012 RTMRel in windows8 RTM, .net framework 4.5

EDIT This bug is now fixed.

Konrad Borowski
  • 11,584
  • 3
  • 57
  • 71
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 1
    Your code works for me. Which version of C#/.Net/Visual Studio are you using? Which platform are you targeting? – jeroenh Oct 10 '12 at 06:55
  • @jeroenh: C# 4.5 VS2012. – Cheng Chen Oct 10 '12 at 06:58
  • I tried with VS2010, C# 4 and 3.5. Don't have access to 2012 right now but will certainly try it tonight. – jeroenh Oct 10 '12 at 07:02
  • Confirmed on VS2012. Gets a 'type argument 'T' violates the constraint of type parameter 'T'.' which is interesting. – leppie Oct 10 '12 at 07:03
  • 2
    Output from PEVerify indicates the compiler has indeed a bug... `[IL]: Error: [ConsoleApplication3.exe : ConsoleApplication3.Derived::<>n__FabricatedMethod1[T]][ offset 0x00000001] Unable to resolve token. 1 Error(s) Verifying ConsoleApplication3.exe` – leppie Oct 10 '12 at 07:14
  • @leppie: LOL I'm just doing the same thing. – Cheng Chen Oct 10 '12 at 07:19
  • 7
    I think, based on the comments so far, you can fairly safely say you've found a bug, and answer your own question with that. The only way in which this could be anything other than a compiler bug is if the language specification doesn't allow `base` method calls in delegates (I haven't checked), but even then, the lack of a proper diagnostic is a missing desirable feature in the compiler. (Do report it to Microsoft, of course.) –  Oct 10 '12 at 07:38
  • BTW, if you remove the `where T : class` constraint from the virtual method. The code verifies, and runs correctly. – leppie Oct 10 '12 at 08:05
  • `where T : struct` also fails, but `where T : IComparable` or `where T : Base` works. Edit: `where T : new()` also fails. – leppie Oct 10 '12 at 08:07
  • 7
    @leppie: Reported to MS, ticket number: 766845 – Cheng Chen Oct 10 '12 at 08:16
  • @DannyChen: :) I added a link back to here for reference. – leppie Oct 10 '12 at 08:19
  • This seems similar to http://stackoverflow.com/questions/5290559/using-base-keyword-in-delegate-causes-system-badimageformatexception, although that caused `BadImageFormatException` and MS claims it has been fixed. – svick Oct 10 '12 at 11:58
  • I find this strange. Isn't the delegate there whether you explicitly use the delegate keyword or not? I thought the two syntaxes were equivalent and am therefore surprised they could lead to different IL unless the actual *parsing* had a bug with more far-reaching consequences. – The Dag Oct 11 '12 at 09:58
  • 1
    @TheDag Doesn't the delegate { } syntax create an extra anonymous method that then calls Foo, where as new Action(base.Foo) calls Foo directly without the extra indirection? – BrandonAGr Oct 15 '12 at 16:06
  • Why do you feel the need to plaster the link and the fixed status all over the question? – BoltClock Dec 14 '12 at 07:56

2 Answers2

3

It is confirmed as a bug and now fixed

Update: the Connect article no longer exists. The bug is fixed.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Cheng Chen
  • 42,509
  • 16
  • 113
  • 174
  • 1
    Pity we will never know when this will reach us. Microsoft should really communicate better on that site... – leppie Dec 14 '12 at 07:55
1

At first - This is a possible way how to fix this, but probably not an answer to your question. (But comments don´t have code formating)

I believe this is similar to this: Outer Variable Trap, because you are using Foo() method as a variable and there is a bug (or maybe a feature) in .NET

I have tried to change Bang() method to this

public void Bang()
{
    Action baseMethod = base.Foo<string>;
    Action bang = new Action(delegate { baseMethod(); });
    bang();    //VerificationException is thrown
}

And it works and the result is "base"

I hope it helps a little bit.

Community
  • 1
  • 1
Ondra
  • 1,619
  • 13
  • 27
  • FYI, Comments *do* have code formatting, you just can't include line breaks and are still limited by the total number of characters (which includes markdown) – Adam Robinson Oct 25 '12 at 15:53