Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

微笑、不失礼 提交于 2019-11-29 01:42:12

问题


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.


回答1:


It is confirmed as a bug and now fixed

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




回答2:


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.



来源:https://stackoverflow.com/questions/12813535/compiler-generated-incorrect-code-for-anonymous-methods-ms-bug-fixed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!