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

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会为它声明个匿名类。

原文地址:https://www.cnblogs.com/363546828/p/2419140.html