c#弱事件(weakevent)

更新时间:2023-06-06 02:34:03 阅读: 评论:0

c#弱事件(weakevent)
传统事件publisher和listener是直接相连的,这样会对垃圾回收带来⼀些问题,例如listener已经不引⽤任何对象但它仍然被publisher引⽤垃圾回收器就不能回收listener所占⽤的内存以及publisher仍然引⽤该listener向其触发事件。
⽽弱事件就解决了这个问题
WeakEventManager是publisher和listener之间的桥梁,从⽽取代两者之间的直接连接
⼀、定义WeakEventManager类
public class WeakCarInfoEventManager : WeakEventManager
{
public static void AddListener(object source,IWeakEventListener listener)
{
CurrentManager.ProtectedAddListener(source, listener);
狗乳腺肿瘤
}
public static void RemoveListener(object source,IWeakEventListener listener)
六年级英语单词{
暴利商品CurrentManager.ProtectedRemoveListener(source, listener);
}
public static WeakCarInfoEventManager CurrentManager
{
get
{
var manager = GetCurrentManager(typeof(WeakCarInfoEventManager))
as WeakCarInfoEventManager;
if(manager == null)
{
必胜客是哪个国家的manager = new WeakCarInfoEventManager();
SetCurrentManager(typeof(WeakCarInfoEventManager), manager);
}
return manager;
我的心愿作文}
}
protected override void StartListening(object source)
{
(source as CarDealer).NewCarInfo += CarDealer_NewCarInfo;
}
void CarDealer_NewCarInfo(object nder,CarInfoEventArgs e)
{
DeliverEvent(nder, e);
}
973项目
protected override void StopListening(object source)
{
(source as CarDealer).NewCarInfo -= CarDealer_NewCarInfo;
}
}
⼆、定义事件监听者(listener)
南车四方
public class Consumer:IWeakEventListener //主要要继承改接⼝
{
private string name;
public Consumer(string name)粉红色玫瑰花语
{
this.name = name;
}
public void NewCarIsHere(object nder,CarInfoEventArgs e)
{
Console.WriteLine("{0}:car {1} is new", name, e.Car);
}
bool IWeakEventListener.ReceiveWeakEvent(Type managerType,object nder,EventArgs e)
{
NewCarIsHere(nder, e as CarInfoEventArgs);
return true;
}
三、事件发布者(publisher)
public class CarInfoEventArgs : EventArgs //⾃定义事件数据参数
{
public CarInfoEventArgs(string car)
{
this.Car = car;
}
public string Car { get; private t; }
}
public class CarDealer
{
public event EventHandler<CarInfoEventArgs> NewCarInfo;
public void NewCar(string car)
{
Console.WriteLine("Car dealer, new car {0}", car);
RaiNewCarInfo(car);
}
protected virtual void RaiNewCarInfo(string car)
{
EventHandler<CarInfoEventArgs> newCarInfo = NewCarInfo;
if (newCarInfo != null)
{
newCarInfo(this, new CarInfoEventArgs(car));
}
}
}
四、在启动函数中通过WeakCarInfoEventManager把publisher和listener联系起来
class Program
{
static void Main(string[] args)
{
var dealer = new CarDealer();
var tian = new Consumer("tian");
WeakCarInfoEventManager.AddListener(dealer, tian);
dealer.NewCar("奔驰");
var tian1 = new Consumer("tian1");
WeakCarInfoEventManager.AddListener(dealer, tian1);
dealer.NewCar("东风");
Console.ReadKey();
}
}
运⾏结果如下:
最后4.5版本已经将weakeventmanager集成进来这样使⽤会更加⽅便
static void Main(string[] args)
{
var dealer = new CarDealer();
var tian = new Consumer("tian");
//WeakCarInfoEventManager.AddListener(dealer, tian);
WeakEventManager<CarDealer, CarInfoEventArgs>.AddHandler(dealer, "NewCarInfo", tian.NewCarIsHere);            dealer.NewCar("奔驰");
var tian1 = new Consumer("tian1");
//WeakCarInfoEventManager.AddListener(dealer, tian1);
WeakEventManager<CarDealer, CarInfoEventArgs>.AddHandler(dealer, "NewCarInfo", tian1.NewCarIsHere);            dealer.NewCar("东风");
Console.ReadKey();
与上⾯的运⾏结果是⼀样的

本文发布于:2023-06-06 02:34:03,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/879193.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:事件   回收   定义   集成   取代   问题   肿瘤
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图