现在的位置: 首页 > 综合 > 正文

Delegate类型和lambda表达式、匿名函数之间的转化

2012年02月03日 ⁄ 综合 ⁄ 共 595字 ⁄ 字号 评论关闭

private void ThreadToMainExeption()
  {
   Thread t = new Thread(() =>
   {
    try
    {
     Console.WriteLine("work thread");
    }
    catch (Exception e)
    {
     this.Invoke((Action)delegate
     {
      throw e;
     });

     //无法将lambda表达式转为delegate类型。
     //this.Invoke(() =>
     //{

     //});

     //无法将匿名方法转为委托类型。
     //this.Invoke(delegate
     //{
     //    throw e;
     //});

     //该段代码可以通过,真矫情。
     //Func<string, string> anonDel = delegate(string param)
     //{
     //    param += "ddd";
     //    return param;
     //};
     //this.Invoke(anonDel);
    }
   });

 

Action是委托实例,而匿名方法只是一个方法,不是对象。但是可以通过类型转化转为对象。

clr会为它声明个匿名类。

抱歉!评论已关闭.