using System;
using System.Runtime.Serialization;
using static System.Console;
namespace Event03
{
class Program
{
static void Main(string[] args)
{
Judgment judgment=new Judgment();
RunSporters sporters=new RunSporters();
judgment.eventRun += sporters.Run;
judgment.eventRun += sporters.RunFail;
judgment.Begin();
ReadKey();
}
}
//发布器(publisher) 是一个包含事件和委托定义的对象。事件和委托之间的联系也定义在这个对象中。
class Judgment
{
public delegate void delegateRun();
public event delegateRun eventRun;
public void Begin()
{
eventRun?.Invoke();
}
}
//订阅者
public class RunSporters
{
public void Run()
{
WriteLine("运动员跑起来了");
}
public void RunFail()
{
WriteLine("有一个起跑失败");
}
}
}
02.标准事件
public delegate void EventHandler(Object sender, EventArgs e);
1.第一个参数sender 用来保存触发事件的对象的引用。由于是Object类型的,所以可以匹配任何类型的实例。
2.第二个参数用来保存有该事件相关的信息
通过扩展EventArgs来传递数据
using System;
using System.Runtime.Serialization;
using static System.Console;
namespace Event03
{
class Program
{
static void Main(string[] args)
{
CarDealer carDealer=new CarDealer();
Consumer consumer=new Consumer("一休");
carDealer.CarEvent += consumer.ConsumerReply;
carDealer.RaiseCarInfo("宝马");
ReadKey();
}
}
public class CarInfoEventArgs:EventArgs
{
public string Car { get; set; }
public CarInfoEventArgs(string car)
{
this.Car = car;
}
}
public class CarDealer
{
public event EventHandler<CarInfoEventArgs> CarEvent;
public void RaiseCarInfo(string car)
{
WriteLine($"new car {car}");
//EventHandler委托的两个参数,第一个参数就是触发事件的事件对象
CarEvent?.Invoke(this,new CarInfoEventArgs(car));
}
}
public class Consumer
{
public string Name;
public Consumer(string name)
{
this.Name = name;
}
//事件处理程序,注意参数和事件的委托EventHandler<CarInfoEventArgs>一样
public void ConsumerReply(object sender,CarInfoEventArgs e)
{
WriteLine($"{Name}:car{e.Car} is Very good!");
}
}
}
标准事件
using System;
using System.Collections.Generic;
using System.Threading;
using static System.Console;
namespace Event07
{
public delegate void BirthDayEventHandler(object sender,BirthdayEventArgs e);
class Program
{
static void Main(string[] args)
{
Me me=new Me("一休");
Friend1 friend1=new Friend1();
Friend2 friend2 = new Friend2();
//订阅事件
me.birthDayEvent+=new BirthDayEventHandler(friend1.SendGift);
me.birthDayEvent += new BirthDayEventHandler(friend2.SendGift);
me.TimeUp();
ReadKey();
}
}
public class BirthdayEventArgs : EventArgs
{
public string Name { get; set; }
public BirthdayEventArgs(string Name)
{
this.Name = Name;
}
}
public class Me
{
public event BirthDayEventHandler birthDayEvent;
private string name;
public Me(string name)
{
this.name = name;
}
//发布事件
public void TimeUp()
{
birthDayEvent?.Invoke(this, new BirthdayEventArgs(name));
}
}
public class Friend1
{
public void SendGift(object sender, BirthdayEventArgs e)
{
Console.WriteLine(e.Name + " 生日到了,我要送礼物");
}
}
public class Friend2
{
public void SendGift(object sender, BirthdayEventArgs e)
{
Console.WriteLine(e.Name + " 生日到了,我要送荔湾");
}
}
}
事件是一个委托链
// 1. 一个被初始化为null的私有委托字段
private BirthDayEventHandle BirthDayEvent =null;
//2\. 一个公共add_BirthDayEvent方法
public void add_BirthDayEvent(BirthDayEventHandle value)
{
// 以一种线程安全的方式从事件中添加一个委托
}
// 3\. 一个公共的remove_BirthDayEvent方法
public void remove_BirthDayEvent(BirthDayEventHandle value)
{
// 以一种线程安全的方式从事件中移除一个委托
}