1.首先上客户端代码
static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); Console.WriteLine("事件注册"); Door door = (Door)ctx.GetObject("door"); door.OnOpen("Opening!"); Console.WriteLine(); Console.ReadLine(); }
spring配置里将做到为door对象的OpenTheDoor事件注入事件的触发代码 触发代码放在Men类的OpenThisDoor方法里
//先定义一个委托 public delegate string OpenHandler(string arg); public class Door { public event OpenHandler OpenTheDoor; public void OnOpen(string arg) { //调用事件 if (OpenTheDoor != null) { Console.WriteLine(OpenTheDoor(arg)); } } } public class Men { public string OpenThisDoor(string arg) { return "参数是:" + arg; } }
最后上配置文件
Spring.net 事件的注入