C#中的事件和代表 [英] Events and delegates in C#

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

问题描述

大家好,

Hi All,

public delegate MyEventHAndler(object o, EventArgs e);

public class EventGenerator
{
   public event MyEventHandler MyEvent;
   ....
   protected virtual void OnMyEvent(EventArgs e)
   {
     if(MyEvent != null)
     {
        MyEvent(this,e);
     }
   }
}

public class MyTest
{
   EventGenerator eventGen;
   public MyTest()
   {
      eventGen = new EventGenerator();
      eventGen.MyEvent += new MyEventHandler(TestFunction); // What happen here intenally 
   }

   public void TestFunction(object o, EventArgs e)
   {
   }
}



下面的代码行在内部发生了什么?



What is happening internally with the following line of code ?

eventGen.MyEvent += new MyEventHandler(TestFunction);



我的问题是...是否有任何数据结构来存储函数的引用?内部正在发生什么? + =的确切含义是什么?



My question is that... is there any data structure to store the reference of the function? what is internally happening? what is += exactly mean ?

推荐答案

加载类时,它将创建一个事件testFunction.
When class will load then it will create a event menthod testFunction.


Event是.NET中的一个特殊对象,它使您可以向外部世界发出通知.从字面上看,.NET事件映射到一个现有的委托,该委托充当其类型,并最终指向一个方法.因此,当事件被订阅时,它需要一种在系统中引发事件时对象将在特定时间调用的方法.因此,基本上,事件是一种将委托调用到类中的结构化方式.

突出显示的EventHandler代码是Notifier,它在执行CallSubroutine方法期间通知事件或引发事件.因此,如果您已订阅事件,则每次调用此方法都会引发该事件.

订阅活动后,您将收到有关该订阅的通知.您可以订阅任意数量的方法,但是每个方法都应遵循Handler的签名.因此,当引发该事件时,该对象将调用预订该事件的所有方法.

希望能解决您的问题.
Event is a special object in .NET which allows you to give notification to the external world. Literally .NET event maps to an existing delegate that acts as a Type of it which eventually points to a method. Thus when the event is subscribed, it needs a method which the object will call at certain time when event is raised in the system. Thus basically an event is a structured way of calling a delegate into a class.

The highlighted EventHandler code is Notifier, which notifies an event or raises an event during the execution of the method CallSubroutine. Hence, if you have subscribed for an event, the event gets raised for each calls to this method.

Once you subscribe to the event, you will get notified for that subscription. You might subscribe as many methods as you want, but each of them should follow the signature of the Handler. So that when, the event is raised, the object will call all the methods subscribed to that event.

Hope, it clears your question.


MyEvent存储订户列表.您可以使用+=将事件处理程序添加到该列表中.触发时,MyEvent调用其列表中的所有事件处理程序.

当您认为不再需要事件处理程序时,也可以通过-=分离事件处理程序.甚至在某些情况下您应该这样做:
您有一个对象MyObject,其中保存有MyEvent的事件处理程序,并且您想摆脱MyObject.由于MyEvent仍在其事件处理程序列表中保留对MyObject的引用,因此MyObject将不会收集垃圾.
您必须先取消订阅该事件,然后才能收集MyObject.
MyEvent stores a list of subscribers. You add your event handler to that list using +=. When firing, MyEvent calls all event handlers in its list.

You can also detach your event handler when you think it''s no longer needed via -=. There is even a scenario when you should do so:
You have an object MyObject which holds an event handler for MyEvent and you want to get rid of MyObject. Since MyEvent still holds a reference to MyObject in its list of event handlers, MyObject will not get garbage collected.
You have to unsubscribe from the event first, then MyObject can be collected.


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

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