⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typewithlotsofevents.cs

📁 Microsoft.NET.框架程序设计修订版中的书中源码
💻 CS
字号:
/******************************************************************************
Module:  TypeWithLotsOfEvents.cs
Notices: Copyright (c) 2002 Jeffrey Richter
******************************************************************************/


using System;
using System.Collections;
using System.Runtime.CompilerServices;


///////////////////////////////////////////////////////////////////////////////


class TypeWithLotsOfEvents {
   // 1. Define a protected instance field that references a collection.
   // The collection manages a set of Event/Delegate pairs.
   // NOTE: The EventHandlerSet type is not part of the FCL.
   // It is my own type, and I describe it later in this chapter.
   protected EventHandlerSet eventSet =
      EventHandlerSet.Synchronized(new EventHandlerSet());

   // 2. Define the members necessary for the Foo event.
   // 2a. Construct a static, read-only object to identify this event.
   // Each object has its own hash code for looking up this
   // event抯 delegate linked list in the object抯 collection.
   protected static readonly Object fooEventKey = new Object();

   // 2b. Define the EventArgs-derived type for this event.
   public class FooEventArgs : EventArgs {}

   // 2c. Define the delegate prototype for this event.
   public delegate void FooEventHandler(Object sender, FooEventArgs e);

   // 2d. Define the event抯 accessor methods that add/remove the
   // delegate from the collection.
   public event FooEventHandler Foo {
      add { eventSet.AddHandler(fooEventKey, value); }
      remove { eventSet.RemoveHandler(fooEventKey, value); }
   }

   // 2e. Define the protected, virtual On method for this event.
   protected virtual void OnFoo(FooEventArgs e) {
      eventSet.Fire(fooEventKey, this, e);
   }

   // 2f. Define the method that translates input to this event.
   public void SimulateFoo() {
      OnFoo(new FooEventArgs());
   }

   // 3. Define the members necessary for the Bar event.
   // 3a. Construct a static, read-only object to identify this event.
   // Each object has its own hash code for looking up this
   // event抯 delegate linked list in the object抯 collection.
   protected static readonly Object barEventKey = new Object();

   // 3b. Define the EventArgs-derived type for this event.
   public class BarEventArgs : EventArgs {}

   // 3c. Define the delegate prototype for this event.
   public delegate void BarEventHandler(Object sender, BarEventArgs e);

   // 3d. Define the event抯 accessor methods that add/remove the
   // delegate from the collection.
   public event BarEventHandler Bar {
      add { eventSet.AddHandler(barEventKey, value); }
      remove { eventSet.RemoveHandler(barEventKey, value); }
   }

   // 3e. Define the protected, virtual On method for this event.
   protected virtual void OnBar(BarEventArgs e) {
      eventSet.Fire(barEventKey, this, e);
   }

   // 3f. Define the method that translates input to this event.
   public void SimulateBar() {
      OnBar(new BarEventArgs());
   }
}


///////////////////////////////////////////////////////////////////////////////


class TypeWithEventInterest {
   public TypeWithEventInterest(TypeWithLotsOfEvents twle) {

      // Register our two callback methods with the events 
      // offerred by the TypeWithLotsOfEvents object
      twle.Foo += new TypeWithLotsOfEvents.FooEventHandler(FooEventCallback);
      twle.Bar += new TypeWithLotsOfEvents.BarEventHandler(BarEventCallback);
   }

   // Called when the TypeWithLotsOfEvents object fires the Foo event
   private void FooEventCallback(Object sender, 
      TypeWithLotsOfEvents.FooEventArgs e) {

      Console.WriteLine("Receiving a Foo notification");
   }

   // Called when the TypeWithLotsOfEvents object fires the Bar event
   private void BarEventCallback(Object sender, 
      TypeWithLotsOfEvents.BarEventArgs e) {

      Console.WriteLine("Receiving a Bar notification");
   }

   public void UnregisterBar(TypeWithLotsOfEvents twle) {
      twle.Bar -= 
         new TypeWithLotsOfEvents.BarEventHandler(BarEventCallback);
   }
}


///////////////////////////////////////////////////////////////////////////////


class App {
   static void Main() {
      // The code here tests the event
      TypeWithLotsOfEvents twle = new TypeWithLotsOfEvents();
      TypeWithEventInterest twei = new TypeWithEventInterest(twle);

      twle.SimulateFoo();
      twle.SimulateBar();
      twei.UnregisterBar(twle);

      twle.SimulateFoo();
      twle.SimulateBar();

      Console.WriteLine("Press <Enter> to terminate this application.");
      Console.ReadLine();
   }
}


//////////////////////////////// End of File //////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -