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; } }最后上配置文件 <!--下面配置文件的意思是men对象为door对象的监听者 当door对象执行OpenTheDoor这个事件则触发的结果则为 men对象的OpenThisDoor 事件参数arg也要有-->
<!--事件注入-->
<object id="men" type="SpringNet_MethodDi.Men, SpringNet_MethodDi"> <listener event="OpenTheDoor" method="OpenThisDoor"> <ref object="door"/> </listener> </object> <object id="door" type="SpringNet_MethodDi.Door, SpringNet_MethodDi" />