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

    1. <small id='7NfKi'></small><noframes id='7NfKi'>

        <bdo id='7NfKi'></bdo><ul id='7NfKi'></ul>

      <tfoot id='7NfKi'></tfoot>

      匿名方法和委托

      时间:2023-11-11

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

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

            • <legend id='nzYkv'><style id='nzYkv'><dir id='nzYkv'><q id='nzYkv'></q></dir></style></legend>

                <tfoot id='nzYkv'></tfoot>

                本文介绍了匿名方法和委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我试图理解为什么 BeginInvoke 方法不接受匿名方法.

                I try to understand why a BeginInvoke method won't accept an anonymous method.

                void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
                {
                    if (InvokeRequired)
                    {
                        //Won't compile
                        BeginInvoke(delegate(object sender, ProgressChangedEventArgs e) 
                        { bgWorker_ProgressChanged(sender, e); });
                    }
                
                    progressBar1.Increment(e.ProgressPercentage);
                }
                

                它告诉我无法从匿名方法"转换为System.Delegate",而当我将匿名方法转换为委托时它确实有效?

                It tells me 'cannot convert from 'anonymous method' to 'System.Delegate' while when I cast the anonymous method to a delegate it does work ?

                BeginInvoke((progressDelegate)delegate { bgWorker_ProgressChanged(sender, e); });
                

                推荐答案

                Delegate 类是委托类型的基类.但是,只有系统和编译器可以显式地从 Delegate 类或 MulticastDelegate 类派生.也不允许从委托类型派生新类型.Delegate 类不被视为委托类型;它是一个用于派生委托类型的类.来源 -- MSDN

                The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types. Source -- MSDN

                因此需要显式强制转换为派生自委托类型.当您为 System.Delegate 类型的参数传递匿名方法时,您会遇到这个特定的编译器错误——幸运的是,这种情况很少见.这太灵活了.

                Hence the need for the explicit cast to a derived-from-Delegate type. You'd encounter this particular compiler error when you pass an anonymous method for a parameter of System.Delegate type - fortunately this is a rare scenario. That's just too much flexibility.

                delegate void MyDelegate();
                
                  static void DoSomething_Flexible(Delegate d)
                  {   d.DynamicInvoke();      }
                  static void DoSomething_Usable(MyDelegate d)
                  {   d();      }
                  static void Main(string[] args)
                  {
                     // requires explicit cast else compile error Error "Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type    
                     DoSomething_Flexible((MyDelegate) delegate { Console.WriteLine("Flexible is here!"); });  
                
                     // Parameter Type is a .NET Delegate, no explicit cast needed here. 
                     DoSomething_Usable(delegate { Console.WriteLine("Usable is here!"); });
                  }
                

                此页面由 Ian Griffith 了解更多信息.(见注释标题后的段落)

                More on this at this page by Ian Griffith. (See the paras after the Notes header)

                这篇关于匿名方法和委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:具有不同参数的两个方法的 C# 委托 下一篇:使用反射在 C# 中引发事件的单元测试

                相关文章

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

                • <bdo id='u7ZFp'></bdo><ul id='u7ZFp'></ul>

              1. <small id='u7ZFp'></small><noframes id='u7ZFp'>

                  1. <tfoot id='u7ZFp'></tfoot>