Compiler generated incorrect code for anonymous methods [MS BUG FIXED](编译器为匿名方法生成了不正确的代码 [MS BUG FIXED])
问题描述
见以下代码:
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(); 抛出异常.在 Bang 方法生成的 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'<class T>.它是一个错误吗?顺便说一句,不使用 delegate{ }(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
编辑我在 windows8 RTM,.net framework 4.5 中使用 VS2012 RTMRel
EDIT I'm using VS2012 RTMRel in windows8 RTM, .net framework 4.5
编辑此错误现已修复.
推荐答案
确认为bug,现已修复
更新:Connect 文章不再存在.错误已修复.
Update: the Connect article no longer exists. The bug is fixed.
这篇关于编译器为匿名方法生成了不正确的代码 [MS BUG FIXED]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:编译器为匿名方法生成了不正确的代码 [MS BUG FIXED]
 
				
         
 
            
        基础教程推荐
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				