<tfoot id='emNaB'></tfoot>
      <bdo id='emNaB'></bdo><ul id='emNaB'></ul>

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

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

        当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式

        Why must a lambda expression be cast when supplied as a plain Delegate parameter(当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式)

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

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

                  本文介绍了当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  取方法System.Windows.Forms.Control.Invoke(Delegate方法)

                  Take the method System.Windows.Forms.Control.Invoke(Delegate method)

                  为什么会出现编译时错误:

                  Why does this give a compile time error:

                  string str = "woop";
                  Invoke(() => this.Text = str);
                  // Error: Cannot convert lambda expression to type 'System.Delegate'
                  // because it is not a delegate type
                  

                  但这工作正常:

                  string str = "woop";
                  Invoke((Action)(() => this.Text = str));
                  

                  什么时候方法需要一个普通的委托?

                  When the method expects a plain Delegate?

                  推荐答案

                  lambda 表达式可以转换为委托类型或表达式树 - 但它必须知道哪个委托类型.仅仅知道签名是不够的.例如,假设我有:

                  A lambda expression can either be converted to a delegate type or an expression tree - but it has to know which delegate type. Just knowing the signature isn't enough. For instance, suppose I have:

                  public delegate void Action1();
                  public delegate void Action2();
                  
                  ...
                  
                  Delegate x = () => Console.WriteLine("hi");
                  

                  您希望 x 引用的对象的具体类型是什么?是的,编译器可以生成具有适当签名的新委托类型,但这很少有用,而且您最终检查错误的机会更少.

                  What would you expect the concrete type of the object referred to by x to be? Yes, the compiler could generate a new delegate type with an appropriate signature, but that's rarely useful and you end up with less opportunity for error checking.

                  如果你想让 Control.Invoke 使用 Action 更容易调用,最简单的方法是向 Control 添加扩展方法:

                  If you want to make it easy to call Control.Invoke with an Action the easiest thing to do is add an extension method to Control:

                  public static void Invoke(this Control control, Action action)
                  {
                      control.Invoke((Delegate) action);
                  }
                  

                  这篇关于当作为普通委托参数提供时,为什么必须强制转换 lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)

                        <legend id='7cILY'><style id='7cILY'><dir id='7cILY'><q id='7cILY'></q></dir></style></legend>
                          <tbody id='7cILY'></tbody>

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