[转] C#中的delegate 和 event

转至:herehtml

终于会用c#中的delegate(委托)

做者:qq826364410程序员

 

引言c#

    Delegate是Dotnet1.0的时候已经存在的特性了,但因为在实际工做中一直没有机会使用Delegate这个特性,因此一直没有对它做整理。这两天,我再度翻阅了一些关于Delegate的资料,并开始正式整理这个C#中著名的特性。本文将由浅入深的谈一下Delegate这个特性。设计模式

一.Delegate是什么?安全

    Delegate中文翻译为“委托”。Msdn中对Delegate的解释以下:异步

    C#中的委托相似于C或C++中的函数指针。使用委托使程序员能够将方法引用封装在委托对象内。而后能够将该委托对象传递给可调用所引用方法的代码,而没必要在编译时知道将调用哪一个方法。与C或C++中的函数指针不一样,委托是面向对象、类型安全的,而且是安全的。函数

    若是你是第一次接触Delegate这个概念,你可能会对上面这段文字感受不知所云,不过没关系,你能够先把Delegate认为就是一个函数指针。spa

    而当你面对一个虚无的概念时,最好的应对方法就是直接看实例。下面一个简单的Delegate使用例子。.net

class Program
    {
        static void OtherClassMethod(){ Console.WriteLine("Delegate an other class's method"); } static void Main(string[] args) {
	    TestDelegate test = new TestDelegate();
test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod);
            test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod);
            test.delegateMethod += Program.OtherClassMethod;
            test.RunDelegateMethods();        
	}    
     }    
class TestDelegate    
     {        
        public delegate void DelegateMethod(); //声明了一个Delegate Type
        public DelegateMethod delegateMethod;   //声明了一个Delegate对象
 
 
        public static void StaticMethod()
        {            
	    Console.WriteLine("Delegate a static method");
 
 
        }        
	public void NonStaticMethod()  
	{            
	    Console.WriteLine("Delegate a non-static method"); 
 
 
        }        
	public void RunDelegateMethods()  
	{            
 
 
	    if(delegateMethod != null)
	    {
                Console.WriteLine("---------");
                delegateMethod.Invoke();    
                Console.WriteLine("---------");
            }
        }
    }

 

上面是一个Delegate的使用例子,运行看看结果吧。下面我稍微解释一下:翻译

【1】public delegate void DelegateMethod();这里声明了一个Delegate的类型,名为DelegateMethod,这种Delegate类型能够搭载:返回值为void,无传入参数的函数。

【2】public DelegateMethod delegateMethod;这里声明了一个DelegateMethod的对象(即,声明了某种Delegate类型的对象)。

区分:DelegateMethod是类型,delegateMethod是对象。

【3】为何上面说Delegate能够看作是函数指针呢?看下面这段代码:

test.delegateMethod = new TestDelegate.DelegateMethod(test.NonStaticMethod); 
test.delegateMethod += new TestDelegate.DelegateMethod(TestDelegate.StaticMethod); test.delegateMethod += Program.OtherClassMethod; 

这里delegateMethod搭载了3个函数,并且能够经过调用delegateMethod.Invoke();运行被搭载的函数。这就是Delegate能够看做为函数指针的缘由。上面这段代码中,delegateMethod只能搭载:返回值为void,无传入参数的函数(见:NonStaticMethod,StaticMethod,OtherClassMethod的定义),这和Delegate类型声明有关(见DelegateMethod的声明:public delegate void DelegateMethod())。

【4】Delegate在搭载多个方法时,能够经过+=增长搭载的函数,也能够经过-=来去掉Delegate中的某个函数。

二.Delegate和C++中函数指针的区别

    Delegate和C++中的函数指针很像,但若是深刻对比,发现其实仍是有区别的,区别主要有三个方面(参考Stanley B. Lippman的一篇文章)

      1) 一个 delegate对象一次能够搭载多个方法(methods),而不是一次一个。当咱们唤起一个搭载了多个方法(methods)的delegate,全部方法以其“被搭载到delegate对象的顺序”被依次唤起。

      2) 一个delegate对象所搭载的方法(methods)并不须要属于同一个类别。一个delegate对象所搭载的全部方法(methods)必须具备相同的原型和形式。然而,这些方法(methods)能够即有static也有non-static,能够由一个或多个不一样类别的成员组成。

      3) 一个delegate type的声明在本质上是建立了一个新的subtype instance,该 subtype 派生自 .NET library framework 的 abstract base classes Delegate 或 MulticastDelegate,它们提供一组public methods用以询访delegate对象或其搭载的方法(methods) ,与函数指针不一样,委托是面向对象、类型安全而且安全的。

    看完上面关于Delegate的介绍,相信你们对它也有所了解了,下面咱们将进行更深刻地讨论!

三.Delegate何时该用?

    看完上面的介绍,你能够会有一些疑问,为何会有Delegate?实际中何时会用到?何时应该去用? 在回答这些问题以前,你们能够先看看下面这段代码:

    class Program
    {
        static void Main(string[] args) { var car = new Car(15); new Alerter(car); car.Run(120); } } class Car { public delegate void Notify(int value); public event Notify notifier; private int petrol = 0; public int Petrol { get { return petrol; } set { petrol = value; if (petrol < 10) //当petrol的值小于10时,出发警报 { if (notifier != null) { notifier.Invoke(Petrol); } } } } public Car(int petrol) { Petrol = petrol; } public void Run(int speed) { int distance = 0; while (Petrol > 0) { Thread.Sleep(500); Petrol--; distance += speed; Console.WriteLine("Car is running... Distance is " + distance.ToString()); } } } class Alerter { public Alerter(Car car) { car.notifier += new Car.Notify(NotEnoughPetrol); } public void NotEnoughPetrol(int value) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("You only have " + value.ToString() + " gallon petrol left!"); Console.ResetColor(); } }

看完了上面的代码后,你可能会问:为何不在public int Petrol中直接调用Alerter.NotEnoughPetrol呢?由于Car模块和Alerter模块自己是两个独立的子系统,若是直接调用,耦合性就会增长,这不是咱们愿意看到的。

    其实以上的代码是设计模式中的观察者模式(观察者模式又称Source/Listener模式)的实现,当汽车在运行中汽油量<10时,警报器便会发出警报。在上面代码中,Delegate至关于一个存放回调函数的函数指针,使用Delegate,咱们能够很是方便地实现观察者模式。而其实,在须要使用回调函数时,咱们均可以考虑使用Delegate。

    不知道你有没有发如今上面的代码中还有一个问题呢?

public event Notify notifier;

上面的代码中,咱们定义了一个Event,而事实上:

public Notify notifier;

 

这样写,也彻底能够知足咱们的需求,这就引出了咱们的另外一个问题,Delegate和Event!

四.Delegate与Event

【1】Delegate和Event的关系

    看微软的代码时,咱们会发现Delegate和Event这两个关键字常常会一块儿出现!究竟他们是什么关系呢?

    在Msdn中,有一段话描述Delegate和Event之间的关系,其实很简单:

        声明事件:若要在类内声明事件,首先必须声明该事件的委托类型。

【2】Delegate和Event配合使用的效果

    看下面几幅图,这是我从一个C#的Application程序截下来的:

 

从上图看到,在响应图形界面的操做中,咱们用到了Event和Delegate,相信这也咱们使用Event和Delegate最频繁的地方了。这里我还想罗嗦一下,平时须要咱们本身写代码的界面事件响应函数,如:button_Click(…),其实都是回调函数,在自动生成的文件Form1.Designer.cs中,VS把事件和其对应的回调函数(即:button_Click(…)等)关联起来,当触发某事件时,对应的回调函数便会执行。

【3】“public Notify notifier”和“public event Notify notifier”的区别

    关于这个问题,咱们直接ildasm看看IL代码吧:>

“public Notify notifier”的IL代码,如图:

 

“public event Notify notifier”的IL代码,如图:

 

差异其实已经很明显了,“public Notify notifier”至关于Class里面的Field,访问级别是public,而“public event Notify notifier”则至关于Property,访问级别是private!因为以上的差异,他们在某些使用上,会稍有不一样,详细的可参考shensr写的《delegate vs. event》。

五.Delegate中的Invoke与BeginInvoke方法

   简单说一下,Invoke与BeginInvoke都是执行Delegate里的搭载函数,而不一样的是:Invoke是一个同步方法,BeginInvoke是一个异步方法。关于这个,有一篇文章《Invoke and BeginInvoke》,对此介绍的比较详细,这里就很少说了。

六.小结   

    回顾一下,到底何时咱们可能会用到Delegate:

【1】.当咱们在C#中须要相似函数指针这样东西时。

【2】.当咱们须要使用回调函数的时候。

【3】.须要异步调用的时候。

【4】.实现观察者模式的时候。

【5】.处理事件响应的时候。

    以上内容均为我的见解,若是有错漏,请各位及时指出:>

相关文章
相关标签/搜索