基于泛型的事件处理演示与净值2005

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于泛型的事件处理演示与净值2005相关的知识,希望对你有一定的参考价值。

Annotated version of the Microsoft/GotDotNet demo from the MSDN Wiki.
  1. //Define a custom EventArgs class to contain your data.
  2. public class MyEventArgs : EventArgs
  3. {
  4. public string info = "data";
  5. }
  6.  
  7. //Declare the event as visible to users of the class
  8.  
  9. public event EventHandler<MyEventArgs> MyEvent;
  10.  
  11.  
  12. //Send the event, note that this will throw a nullref if there are no subscribers
  13.  
  14. //Internal version prevents outsiders from needing to know about the contents of MyEventArgs
  15. protected virtual void InternalSendMyEvent(CustomEventArgs e)
  16. {
  17. if(MyEvent != null)
  18. {
  19. e.info = "Hello Events!";
  20. //This calls all registered subscribers with the following parameters.
  21. MyEvent(this, e);
  22. }
  23. }
  24.  
  25. //Public version to allow outsiders to trigger the event, not typical implementation.
  26. public void CreateEvent()
  27. {
  28. InternalSendMyEvent(new MyEventArgs());
  29. }
  30.  
  31.  
  32.  
  33. //Consumer
  34. //Register the Handler
  35.  
  36. eventRaiser.MyEvent += new EventHandler<CustomEventArgs>(HandleEvent);
  37.  
  38. //Define the Handler
  39. private void HandleEvent(object sender, MyEventArgs e)
  40. {
  41. MessageBox.Show("Event handled:" + e.info);
  42. }
  43.  
  44.  
  45. //Cause the Event
  46. eventRaiser.CreateEvent();

以上是关于基于泛型的事件处理演示与净值2005的主要内容,如果未能解决你的问题,请参考以下文章

集合泛型的使用

C#规范整理·泛型委托事件

深入 Java 泛型一泛型的作用与定义

泛型的处理

Java基础之泛型的使用

JAVA中,关于可变参数和泛型的问题。