下载app免费领取会员
先看一个普通事件例子:
class Program
{
static void Main(string[] args)
{
CarDealer dealer = new CarDealer();
Consumer micheal = new Consumer("Micheal");
dealer.NewCarInfo += micheal.NewCarIsHere;
Consumer sebastian = new Consumer("Sebastian");
dealer.NewCarInfo += sebastian.NewCarIsHere;
dealer.NewCar("Ferrari");
sebastian = null;
GC.Collect();
dealer.NewCar("Ferrari");//两次输出是相同的,说明sebastianb并没有回收掉
Console.ReadLine();
}
}
public class CarInfoEventArgs:EventArgs
{
public CarInfoEventArgs(string car)
{
Car = car;
}
public string Car { get; private set; }
}
public class CarDealer
{
public event EventHandler<CarInfoEventArgs> NewCarInfo;
public void NewCar(string car)
{
Console.WriteLine("CarDealer, new car {0}", car);
RaiseNewCarInfo(car);
}
protected virtual void RaiseNewCarInfo(string car)
{
if(NewCarInfo!=null)
{
NewCarInfo(this, new CarInfoEventArgs(car));
}
}
}
public class Consumer
{
private string _name = string.Empty;
public Consumer(string name)
{
_name = name;
}
public void NewCarIsHere(object sender,CarInfoEventArgs e)
{
Console.WriteLine("{0}:Car {1} is new", _name, e.Car);
}
}
通过上面红色字发现普通事件的垃圾回收是有一个问题的,当然这个是可以通过取消订阅解决的,但是有没有更智能一点的办法呢?
下面看看弱事件的效果:
class Program
{
static void Main(string[] args)
{
CarDealer dealer = new CarDealer();
Consumer micheal = new Consumer("Micheal");
GC.Collect();
WeakCarInfoEventManager.AddListener(dealer, micheal);
Consumer sebastian = new Consumer("Sebastian");
WeakCarInfoEventManager.AddListener(dealer, sebastian);
dealer.NewCar("Ferrari");
sebastian = null;
GC.Collect();
dealer.NewCar("Ferrari");//不再输出sebastian的内容,说明sebastian已经回收了
Console.ReadLine();
}
}
public class CarInfoEventArgs : EventArgs
{
public CarInfoEventArgs(string car)
{
Car = car;
}
public string Car { get; private set; }
}
public class CarDealer
{
public event EventHandler<CarInfoEventArgs> NewCarInfo;
public void NewCar(string car)
{
Console.WriteLine("CarDealer, new car {0}", car);
RaiseNewCarInfo(car);
}
protected virtual void RaiseNewCarInfo(string car)
{
if (NewCarInfo != null)
{
NewCarInfo(this, new CarInfoEventArgs(car));
}
}
}
public class Consumer:IWeakEventListener
{
private string _name = string.Empty;
public Consumer(string name)
{
_name = name;
}
public void NewCarIsHere(object sender, CarInfoEventArgs e)
{
Console.WriteLine("{0}:Car {1} is new", _name, e.Car);
}
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
NewCarIsHere(sender, e as CarInfoEventArgs);
return true;
}
}
public class WeakCarInfoEventManager : WeakEventManager
{
private WeakCarInfoEventManager()
{
}
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
{
WeakCarInfoEventManager _currentManager = GetCurrentManager(typeof(WeakCarInfoEventManager)) as WeakCarInfoEventManager;
if (_currentManager == null)
{
_currentManager = new WeakCarInfoEventManager();
SetCurrentManager(typeof(WeakCarInfoEventManager), _currentManager);
}
return _currentManager;
}
}
protected override void StartListening(object source)
{
(source as CarDealer).NewCarInfo += WeakCarInfoEventManager_NewCarInfo;
}
private void WeakCarInfoEventManager_NewCarInfo(object sender, CarInfoEventArgs e)
{
DeliverEvent(sender, e);
}
protected override void StopListening(object source)
{
(source as CarDealer).NewCarInfo -= WeakCarInfoEventManager_NewCarInfo;
}
}
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。