匿名方法和代表 [英] Anonymous methods and delegates

查看:163
本文介绍了匿名方法和代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解为什么一个BeginInvoke方法将不接受匿名方法。

 无效bgWorker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
    如果(InvokeRequired)
    {
        //不能编译
        的BeginInvoke(委托(对象发件人,ProgressChangedEventArgs E)
        {bgWorker_ProgressChanged(发件人,E); });
    }

    progressBar1.Increment(e.ProgressPercentage);
}
 

它告诉我匿名方法'到'System.Delegate''不能从转换'而当我投匿名方法到委托它的工作?

 的BeginInvoke((progressDelegate)委托{bgWorker_ProgressChanged(发件人,E);});
 

解决方案

委托类是委托类型的基类。然而,只有系统和编译器可以从Delegate类或从MulticastDelegate类显式派生。这也是不允许来源于委托类型的新类型。 委托类不被视为一个委托类型;它是用于导出代表类型的类。 来源 - MSDN

因此​​,需要明确的强制转换派生自-委托类型。你会遇到这种特殊的编译器错误,当你为System.Delegate类型的参数传递一个匿名方法 - 幸运的是,这是一个罕见的情景。这只是太多的灵活性。

 委托无效MyDelegate();

  静态无效DoSomething_Flexible(代表D)
  {d.DynamicInvoke(); }
  静态无效DoSomething_Usable(MyDelegate D)
  {D(); }
  静态无效的主要(字串[] args)
  {
     //需要显式转换其他编译错误错误无法转换匿名方法键入'System.Delegate',因为它不是一个委托类型
     DoSomething_Flexible((MyDelegate)委托{Console.WriteLine(柔性就在这里!);});

     //参数类型是一个.NET委托,在这里不需要显式类型转换。
     DoSomething_Usable(委托{Console.WriteLine(可用的就在这里!);});
  }
 

更多关于这在此页由伊恩·格里菲斯。 (见注释头之后的第)

I try to understand why a BeginInvoke method won't accept an anonymous method.

void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    if (InvokeRequired)
    {
        //Won't compile
        BeginInvoke(delegate(object sender, ProgressChangedEventArgs e) 
        { bgWorker_ProgressChanged(sender, e); });
    }

    progressBar1.Increment(e.ProgressPercentage);
}

It tells me 'cannot convert from 'anonymous method' to 'System.Delegate' while when I cast the anonymous method to a delegate it does work ?

BeginInvoke((progressDelegate)delegate { bgWorker_ProgressChanged(sender, e); });

解决方案

The Delegate class is the base class for delegate types. However, only the system and compilers can derive explicitly from the Delegate class or from the MulticastDelegate class. It is also not permissible to derive a new type from a delegate type. The Delegate class is not considered a delegate type; it is a class used to derive delegate types. Source -- MSDN

Hence the need for the explicit cast to a derived-from-Delegate type. You'd encounter this particular compiler error when you pass an anonymous method for a parameter of System.Delegate type - fortunately this is a rare scenario. That's just too much flexibility.

delegate void MyDelegate();

  static void DoSomething_Flexible(Delegate d)
  {   d.DynamicInvoke();      }
  static void DoSomething_Usable(MyDelegate d)
  {   d();      }
  static void Main(string[] args)
  {
     // requires explicit cast else compile error Error "Cannot convert anonymous method to type 'System.Delegate' because it is not a delegate type    
     DoSomething_Flexible((MyDelegate) delegate { Console.WriteLine("Flexible is here!"); });  

     // Parameter Type is a .NET Delegate, no explicit cast needed here. 
     DoSomething_Usable(delegate { Console.WriteLine("Usable is here!"); });
  }

More on this at this page by Ian Griffith. (See the paras after the Notes header)

这篇关于匿名方法和代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆