遇到一本好书,终于把事件搞懂了。
using System;
class Sender
{
private int myInt;//字段
public int MyInt//属性
{
get{return myInt;} //获取get
set
{
myInt = value;//赋值
//Whenever we set a new value, the event will fire.
//就是每次给字段赋值后,触发事件
OnMyIntChanged();
}
}
//啰嗦解释一下:这里是"声明事件",事件实际上就是委托,委托就是类型安全的函数指针。
//前面有关键字event+关键字EventHandler组成
public event EventHandler MyIntChanged;
public void OnMyIntChanged() //配套的事件方法,前面加了On字。
{
if(MyIntChanged != null)
{
MyIntChanged(this, EventArgs.Empty);
}
}
//可以被事件关联的方法,但是需要遵循事件标准格式,就是参数里面必须有
//object sender,System.EventArgs e 这两个参数
//设置好标准参数,关联好方法,事件就可以"为所欲为了"。
public void GetNotificationItself(Object sender, System.EventArgs e)
{
Console.WriteLine("Sender himself send a notification: 我已经改变了myint的值为{0} ", myInt);
}
}
class Receiver
{
public void GetNotificationFromSender(Object sender, System.EventArgs e)
{
Console.WriteLine("Receiver类 receives a notification: Sender类最近改变了myInt的值 . ");
}
}
class Program
{
static void Main(string[] args)
{
//实例化Sender类
Sender sender = new Sender();
//实例化Receiver类
Receiver receiver = new Receiver();
//Receiver is registering for a notification from sender
//事件实际上就是委托,委托就是类型安全的函数指针。
//所以此时,sender的MyIntChanged委托,关联到receiver.GetNotificationFromSender方法。
sender.MyIntChanged += receiver.GetNotificationFromSender;
//接下去的步骤:
//①调用属性的set,set里面就有触发的事件函数OnMyIntChanged();
//②触发事件函数OnMyIntChanged(),里面再次MyIntChanged(this, EventArgs.Empty);一下
//③此时委托一下,this就是类receiver,事件参数消息设置为空EventArgs.Empty;
//④运行Console.WriteLine("Receiver receives a notification: Sender recently has changed the myInt value . ");
sender.MyInt = 1;//Receiver receives a notification: Sender类最近改变了 myInt 的值 . "
sender.MyInt = 2;//"Receiver receives a notification: Sender类最近改变了 myInt 的值 . "
//Unregistering now,名词叫做反注册,实际就是删除引用关联(函数指针重新设置为null)
sender.MyIntChanged -= receiver.GetNotificationFromSender;
//No notification sent for the receiver now.再也不会调用GetNotificationFromSender了
sender.MyInt = 3;
//===========================================================
//===========================================================
//事件(委托)再次关联sender.GetNotificationItself
sender.MyIntChanged += sender.GetNotificationItself;
sender.MyInt = 4;//"Sender himself send a notification: 我已经改变了myint的值为 4";
}
}
前面讲到事件用event+EventHandler组成,这个EventHandler是默认的类型。下面让我们自定义事件指针类型。
前面有关键字event+关键字EventHandler组成
using System;
//Create a delegate.声明委托MyIntChangedEventHandler,其中的参数eventArgs,默认类型是EventArgs
//自己定义的类型为JobNoEventArgs,为声明事件做基础准备
delegate void MyIntChangedEventHandler(Object sender, JobNoEventArgs eventArgs);
//事件的EventArgs参数,本身这个参数是类,可以做基类。自定义类JobNoEventArgs继承它。
class JobNoEventArgs: EventArgs
{
int jobNo = 0; //字段 jobNo 默认为 0
public int JobNo
{
get{return jobNo;}
set{jobNo = value;}
}
}
class Sender
{
//声明事件,格式:public event + 自定义委托类型 MyIntChangedEventHandler
public event MyIntChangedEventHandler MyIntChanged; //事件配套①
private int myInt; //字段myInt
public int MyInt//属性MyInt
{
get{return myInt;}
set
{
myInt = value;
//Raise the event.触发事件
//Whenever you set a new value, the event will fire.
OnMyIntChanged();
}
}
//事件配套②,就是需要在MyIntChanged前面加On,实现事件的触发方法,
protected virtual void OnMyIntChanged()
{
if(MyIntChanged != null)
{
//先实例化 JobNoEventArgs
JobNoEventArgs jobNoEventArgs = new JobNoEventArgs();
jobNoEventArgs.JobNo = myInt;
MyIntChanged(this, jobNoEventArgs);
}
}
}
//Create a Receiver or Subscriber for the event.
//事件的接收者,或者叫做订阅。其实就是关联函数指针到这个方法。这个方法需要规定有Object sender, JobNoEventArgs e
//这两个参数,并且函数签名要和delegates MyIntChangedEventHandler 的签名一致,此时都为void
class Receiver
{
public void GetNotificationFromSender(Object sender, JobNoEventArgs e)
{
Console.WriteLine("Receiver receives a notification: Sender recently has changed the myInt value to {0}.",e.JobNo);
}
}
//Create a Receiver2 or Subscriber for the event.
class Receiver2
{
public void GetNotificationFromSender(Object sender, JobNoEventArgs e)
{
Console.WriteLine("Receiver2收到到消息,通过sender类改变了值myint为 {0}.",e.JobNo.ToString()+"+你妈逼!");
}
}
class Program
{
static void Main(string[] args)
{
Sender sender = new Sender(); //实例化具有事件声明+事件方法触发器的类,Shender
Receiver receiver = new Receiver();//实例化具有可以让事件指针关联的方法的类,Receiver
//Receiver is registering for a notification from sender
sender.MyIntChanged += receiver.GetNotificationFromSender; //关联指针
sender.MyInt = 1;//这个1最终结果会传递给类型为JobNoEventArgs的参数e
sender.MyInt = 2;//这个2最终结果会传递给类型为JobNoEventArgs的参数e
//Unregistering now //反注册,释放指针
sender.MyIntChanged -= receiver.GetNotificationFromSender;
//No notification sent for the receiver now.
sender.MyInt = 3;
//自己改着玩,e参数有实际内容,所以可以乱改!
Receiver2 receiver2 = new Receiver2();
sender.MyIntChanged += receiver2.GetNotificationFromSender;
sender.MyInt = 4;
}
}