1.声明事件参数
public class ItemEventArgs<T> : EventArgs
{
public ItemEventArgs(T item)
{
Item = item;
}
public T Item { get; protected set; }
}
2.声明事件委托
public delegate void ItemEventHandler<T>(object sender, ItemEventArgs<T> e);
3.
public event ItemEventHandler<DataPacket> PacketReceived;
4.
protected virtual void OnPacketReceived(ItemEventArgs<DataPacket> e)
{
if (PacketReceived != null) PacketReceived(this, e);
}
5.
OnPacketReceived(new ItemEventArgs<DataPacket>(_packet));
6
改进
public static implicit operator ItemEventArgs<T>(T item)
{
return new ItemEventArgs<T>(item);
}
7.
OnPacketReceived(_packet);
原文地址