简单.net事件模型

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
publisher p
= new publisher("P");
Receive r
= new Receive();
p.Sayhello
+= new publisher.SayhelloEventHandler(r.tip);
p.OnSayhello(
"OK");
}


}
public class Receive
{
public void tip(String s)
{
Console.WriteLine(
"Reciver Heard: " + s);
}
}

public class publisher
{
public String Name { get; set; }
public publisher() { }
public publisher(String name) { this.Name = name; }
public delegate void SayhelloEventHandler(String s);
public event SayhelloEventHandler Sayhello;

public void OnSayhello(String s)
{
if (this.Sayhello != null)
{
Console.WriteLine(
"PUBLISH Say " + s);
Sayhello(s);
}

}
}

}

 

posted @ 2011-01-06 09:43  Devin Mao  阅读(150)  评论(0编辑  收藏  举报