编译器生成匿名方法不正确的代码[MS BUG固定] [英] Compiler generated incorrect code for anonymous methods [MS BUG FIXED]

查看:204
本文介绍了编译器生成匿名方法不正确的代码[MS BUG固定]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的代码:

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



新派生的()邦(); 抛出异常。里面的方法我得到的生成CIL:

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
}

我认为正确的代码应该是'<> n__FabricatedMethod1 '<类T> 。它是一个错误吗?
顺便说一句,不使用委托{} (lambda表达式是一样的),代码工作正常语法糖。

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



修改我使用VS2012 RTMRel在windows8的RTM, .NET框架4.5

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

修改:此错误是现在固定:的 http://connect.microsoft.com/VisualStudio/feedback/details/766845/vs2012-在编译器生成的,不正确码换匿名方法

EDIT This bug is now fixed: http://connect.microsoft.com/VisualStudio/feedback/details/766845/vs2012-the-compiler-generated-incorrect-codes-for-anonymous-methods

推荐答案

据证实为错误现在固定:的 http://connect.microsoft.com/VisualStudio/feedback/details/766845/vs2012-the-compiler-generated-incorrect-codes-for-anonymous-methods

It is confirmed as a bug and now fixed: http://connect.microsoft.com/VisualStudio/feedback/details/766845/vs2012-the-compiler-generated-incorrect-codes-for-anonymous-methods

这篇关于编译器生成匿名方法不正确的代码[MS BUG固定]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆