<i id='VJXkD'><tr id='VJXkD'><dt id='VJXkD'><q id='VJXkD'><span id='VJXkD'><b id='VJXkD'><form id='VJXkD'><ins id='VJXkD'></ins><ul id='VJXkD'></ul><sub id='VJXkD'></sub></form><legend id='VJXkD'></legend><bdo id='VJXkD'><pre id='VJXkD'><center id='VJXkD'></center></pre></bdo></b><th id='VJXkD'></th></span></q></dt></tr></i><div id='VJXkD'><tfoot id='VJXkD'></tfoot><dl id='VJXkD'><fieldset id='VJXkD'></fieldset></dl></div>
<legend id='VJXkD'><style id='VJXkD'><dir id='VJXkD'><q id='VJXkD'></q></dir></style></legend>
        • <bdo id='VJXkD'></bdo><ul id='VJXkD'></ul>

        <small id='VJXkD'></small><noframes id='VJXkD'>

        <tfoot id='VJXkD'></tfoot>

        编译器为匿名方法生成了不正确的代码 [MS BUG FIXED]

        Compiler generated incorrect code for anonymous methods [MS BUG FIXED](编译器为匿名方法生成了不正确的代码 [MS BUG FIXED])
      1. <legend id='NnwYr'><style id='NnwYr'><dir id='NnwYr'><q id='NnwYr'></q></dir></style></legend>

            • <bdo id='NnwYr'></bdo><ul id='NnwYr'></ul>
                <i id='NnwYr'><tr id='NnwYr'><dt id='NnwYr'><q id='NnwYr'><span id='NnwYr'><b id='NnwYr'><form id='NnwYr'><ins id='NnwYr'></ins><ul id='NnwYr'></ul><sub id='NnwYr'></sub></form><legend id='NnwYr'></legend><bdo id='NnwYr'><pre id='NnwYr'><center id='NnwYr'></center></pre></bdo></b><th id='NnwYr'></th></span></q></dt></tr></i><div id='NnwYr'><tfoot id='NnwYr'></tfoot><dl id='NnwYr'><fieldset id='NnwYr'></fieldset></dl></div>
                  <tfoot id='NnwYr'></tfoot>
                • <small id='NnwYr'></small><noframes id='NnwYr'>

                    <tbody id='NnwYr'></tbody>
                  本文介绍了编译器为匿名方法生成了不正确的代码 [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]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                • <i id='YKHVQ'><tr id='YKHVQ'><dt id='YKHVQ'><q id='YKHVQ'><span id='YKHVQ'><b id='YKHVQ'><form id='YKHVQ'><ins id='YKHVQ'></ins><ul id='YKHVQ'></ul><sub id='YKHVQ'></sub></form><legend id='YKHVQ'></legend><bdo id='YKHVQ'><pre id='YKHVQ'><center id='YKHVQ'></center></pre></bdo></b><th id='YKHVQ'></th></span></q></dt></tr></i><div id='YKHVQ'><tfoot id='YKHVQ'></tfoot><dl id='YKHVQ'><fieldset id='YKHVQ'></fieldset></dl></div>

                  <small id='YKHVQ'></small><noframes id='YKHVQ'>

                      <bdo id='YKHVQ'></bdo><ul id='YKHVQ'></ul>
                      <tfoot id='YKHVQ'></tfoot>
                          <tbody id='YKHVQ'></tbody>
                        <legend id='YKHVQ'><style id='YKHVQ'><dir id='YKHVQ'><q id='YKHVQ'></q></dir></style></legend>