【问题标题】:lambda expression and Messagebox in C#C# 中的 lambda 表达式和消息框
【发布时间】:2013-08-22 13:00:23
【问题描述】:
private void SimpleLambda()
{
  dynamic showMessage =  x => MessageBox.Show(x);

  showMessage("Hello World!");
}

错误消息是: 无法将 lambda 表达式转换为动态类型,因为它不是委托类型

任何帮助,

【问题讨论】:

  • 或Action<string>或Func<string, DialogResult>
  • 一般来说,我建议您将dynamic 视为在普通代码中应避免使用的高级功能。

标签: c# linq dynamic lambda messagebox


【解决方案1】:

这与 MessageBox 无关 - 正如错误消息所述,您根本无法将 lambda 表达式转换为 dynamic,因为编译器不知道要为其创建实例的委托类型。

你想要:

Action<string> action = x => MessageBox.Show(x);

或者甚至使用方法组转换,尽管那样你必须匹配返回类型:

Func<string, DialogResult> func = MessageBox.Show;

如果你愿意,然后可以使用dynamic:

dynamic showMessage = action; // Or func
showMessage("Hello World!");

或者,您可以在显式委托实例表达式中指定 lambda 表达式:

dynamic showMessage = new Action<string>(x => MessageBox.Show(x));

【讨论】:

  • @speti43:正如我在回答中所说:“编译器不知道创建一个实例的委托类型”
  • @speti43 因为 Show 有返回类型
  • @Mario:只有方法组转换位会失败 - lambda 表达式很好。如果您理解这一点,那么这样说会很有帮助......以及提到开始的原因:) 我已经修复了方法组转换部分。
  • 我明白了,问题是当您将委托实例分配给动态变量时。
  • @Sangram - 你应该根据你是否相信答案是否有用来投票,而不是谁写的,虽然这是一个很好的答案。
【解决方案2】:
private void SimpleLambda()
{
  Action<string> showMessage =  x => MessageBox.Show(x);

  showMessage("Hello World!");
}

【讨论】:

    【解决方案3】:

    您必须声明委托类型。否则它不会知道它应该是什么类型的 lambda 表达式——x 可以是任何东西。这应该有效:

    Action<string> showMessage = x => MessageBox.Show(x);
    

    请参阅 Action&lt;T&gt; 以了解此委托类型是什么。

    【讨论】:

      【解决方案4】:

      我为此创建了一个类型推断助手。如果我想将它们存储在临时变量中,我真的不喜欢输入 lambdas 的签名,所以我写了

      var fn = Func.F( (string x) => MessageBox.Show(x) );
      

      或

      var fn = Func.F( (double x, double y) => x + y );
      

      您仍然需要输入参数签名,但您让类型推断处理返回类型。

      实现是

      using System;
      
      namespace System
      {
          /// <summary>
          /// Make type inference in C# work harder for you. Normally when
          /// you want to declare an inline function you have to type
          /// 
          ///     Func<double, double, double> fn = (a,b)=>a+b
          /// 
          /// which sux! With the below methods we can write
          /// 
          ///     var fn = Func.F((double a, double b)=>a+b);
          ///
          /// which is a little better. Not as good as F# type
          /// inference as you still have to declare the args
          /// of the function but not the return value which
          /// is sometimes not obvious straight up. Ideally
          /// C# would provide us with a keyword fun used like
          /// 
          ///     fun fn = (double a, double b)=>a+b;
          ///
          /// but till then this snippet will make it easier
          /// 
          /// </summary>
          public static class Func
          {
              public static Func<A> F<A>(Func<A> f)
              {
                  return f; 
              }
              public static Func<A,B> F<A, B>(Func<A, B> f)
              {
                  return f; 
              }
              public static Func<A,B,C> F<A, B,C>(Func<A, B,C> f)
              {
                  return f; 
              }
              public static Func<A,B,C,D> F<A,B,C,D>(Func<A,B,C,D> f)
              {
                  return f; 
              }
              public static Func<A,B,C,D,E> F<A,B,C,D,E>(Func<A,B,C,D,E> f)
              {
                  return f; 
              }
      
              public static Action A(Action f)
              {
                  return f; 
              }
              public static Action<_A> A<_A>(Action<_A> f)
              {
                  return f; 
              }
              public static Action<_A,B> A<_A, B>(Action<_A, B> f)
              {
                  return f; 
              }
              public static Action<_A,B,C> A<_A, B,C>(Action<_A, B,C> f)
              {
                  return f; 
              }
              public static Action<_A,B,C,D> A<_A,B,C,D>(Action<_A,B,C,D> f)
              {
                  return f; 
              }
              public static Action<_A,B,C,D,E> A<_A,B,C,D,E>(Action<_A,B,C,D,E> f)
              {
                  return f; 
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-19
        • 2017-10-02
        • 2011-01-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        相关资源
        最近更新 更多