2013-08-22 48 views
5
private void SimpleLambda() 
{ 
    dynamic showMessage = x => MessageBox.Show(x); 

    showMessage("Hello World!"); 
} 

的错误信息是: 不能lambda表达式转换为动态类型,因为它不是一个委托类型lambda表达式和消息框在C#

任何帮助,

+0

或'行动'或'Func键<字符串的DialogResult>' –

+0

在一般情况下,我建议你把'dynamic'在正常的代码要避免的高级功能。 – Brian

回答

5
private void SimpleLambda() 
{ 
    Action<string> showMessage = x => MessageBox.Show(x); 

    showMessage("Hello World!"); 
} 
4

你必须申报代表类型。否则它不会知道它应该是什么类型的lambda表达式— x可能是任何东西。这应该工作:

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

请参阅Action<T>澄清这种代表类型是什么。

16

这与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!"); 

或者,您可以s pecify lambda表达式中明确委托实例表达:

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

+1,因为Jon Skeet回答:) –

+0

-1原因这不适用于“动作” –

+0

为什么不行? – speti43

1

我创建了一个类型推断帮手这一点。我真的不喜欢打字了lambda表达式的签名,如果我想将它们存储在临时变量,所以我写

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

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

你还必须把参数signiture但你让类型推理处理返回类型。

实施是

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; 
     } 
    } 

}