class Publish
{
public event EventHandler eventName; //declare event named eventName
public void inspireEvent() // inspire the event
{
onEventName(new EventArgs());
}
public void onEventName(EventArgs e) // will use the scriber‘s method to reply the event
{
EventHandler handle = eventName;
if (handle != null)
{
handle(this,e);
}
}
}class Scriber
{
public Scriber(Publish pb)
{
pb.eventName += howDo; // scribe the event
}
public void howDo(Object sender, EventArgs e)
{
Console.WriteLine("what a fuck day it is");
}
}class Program
{
static void Main(string[] args)
{
Publish ph = new Publish();
Scriber sb = new Scriber(ph);
ph.inspireEvent(); //motivate the event
}
}
EventHandler handle = eventBase; eventBase(obj,e);只能出现 eventBase+=,eventBase-=。
class BaseEvent
{
public event EventHandler baseEvent;
public virtual void draw()
{
Console.WriteLine("This is in base draw");
}
public virtual void onDraw(EventArgs e)
{
EventHandler handle = baseEvent;
if (handle != null)
{
handle(this ,e);
}
Console.WriteLine("this is in base on");
}
}
class Derived : BaseEvent
{
public override void draw()
{
//base.draw();
Console.WriteLine("this is in derived class");
}
public override void onDraw(EventArgs e)
{
//EventHandler handle = baseEvent;
//baseEvent(this, e);
base.onDraw(e);
}
public void motivateDraw()
{
onDraw(new EventArgs());
}
}class Scribe
{
public Scribe(Derived dr)
{
dr.baseEvent += Motive;
}
public void Motive(Object sender, EventArgs e)
{
Console.WriteLine("what a fuck day");
}
}class Program
{
static void Main(string[] args)
{
Derived D = new Derived();
BaseEvent B = new BaseEvent();
Scribe Sb = new Scribe(D);
D.motivateDraw();
}
}
namespace ConsoleApplication16InterfaceEvent
{
interface Iface
{
event EventHandler faceEvent;
}
interface Ifoot
{
event EventHandler footEvent;
}
interface Ihand
{
event EventHandler footEvent;
}
class InterEvent : Iface,Ifoot,Ihand
{
public event EventHandler faceEvent;
//public event EventHandler footEvent;
public event EventHandler preFootEvent;
public event EventHandler nexHandEvent;
Object objectLock = new Object();
event EventHandler Ifoot.footEvent
{
add
{
lock (objectLock)
{
preFootEvent += value;
}
}
remove
{
lock (objectLock)
{
preFootEvent -= value;
}
}
}
event EventHandler Ihand.footEvent
{
add
{
lock (objectLock)
{
nexHandEvent += value;
}
}
remove
{
lock (objectLock)
{
nexHandEvent -= value;
}
}
}
public void finger()
{
EventHandler handle = preFootEvent;
if ( handle != null)
preFootEvent(this,new EventArgs());
handle = nexHandEvent;
if (handle != null)
nexHandEvent(this,new EventArgs());
}
}
}
//...............
namespace ConsoleApplication16InterfaceEvent
{
class Scribe
{
public Scribe(InterEvent Et)
{
Ifoot foot = (Ifoot)Et;
foot.footEvent += draw1;
Ihand hand = (Ihand)Et;
hand.footEvent += draw2;
}
void draw1(Object sender, EventArgs e)
{
Console.WriteLine("this is in Scribe draw1");
}
void draw2(Object sender, EventArgs e)
{
Console.WriteLine("this is in Scribe draw2");
}
}
}//..................
namespace ConsoleApplication16InterfaceEvent
{
class Program
{
static void Main(string[] args)
{
InterEvent EI = new InterEvent();
Scribe Sb = new Scribe(EI);
EI.finger();
}
}
}原文:http://blog.csdn.net/ddupd/article/details/22101661