//自定义消息
public class CustomEventArgs : EventArgs
{
public readonly string _msg;
public CustomEventArgs(string msg)
{
this._msg = msg;
}
}
//自定义事件
public delegate void CustomEventHandler(object sender, CustomEventArgs e);
//小龙女类
public class XiaolongConsole
{
private string msg = "我是小龙女";
public event CustomEventHandler CustomEvent;//自定义事件
public void WriteLetter()//发消息
{
CustomEventArgs args = new CustomEventArgs(msg);
CustomEvent(this, args);
}
}
//杨过类
public class YangGuoConsole
{
//处理消息
public void ProcessLetter(object sender, CustomEventArgs e)
{
XiaolongConsole XiaolongConsole = sender as XiaolongConsole;
if (XiaolongConsole != null)
{
//处理
}
}
}
Main()
{
XiaolongConsole xiaolong = new XiaolongConsole();
YangGuoConsole yangGuo = new YangGuoConsole();
xiaolong.CustomEvent+=yangGuo.ProcessLetter;
xiaolong .WriteLetter();
}