using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A10_IndepthStudyDelegate
{
//定义委托
public delegate void InformInfoHandler(string name);
class Invoke
{
//声明事件(委托实例)
public static event InformInfoHandler eveInfo;
//方法调用
public void DisplayInfo()
{
if (eveInfo != null)
{
eveInfo.Invoke("Test Message Content ...");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A10_IndepthStudyDelegate
{
class Registe
{
public Registe()
{
Invoke.eveInfo += InformZhangSan;
Invoke.eveInfo += InformLisi;
Invoke.eveInfo += InformWangWu;
}
public void InformZhangSan(string name)
{
Console.WriteLine("{0}Message for ZhangSan ...",name);
}
public void InformLisi(string name)
{
Console.WriteLine("{0}Message for Lisi ...", name);
}
public static void InformWangWu(string name)
{
Console.WriteLine("{0}Message for WangWu ...", name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace A10_IndepthStudyDelegate
{
class Test
{
static void Main1(string[] args)
{
//运行事件的注册
new Registe();
//运行调用方
Invoke obj = new Invoke();
obj.DisplayInfo();
}
}
}