/// <summary> /// 发送一个指定类型的 command /// </summary> public void Dispatch(Type type, params object[] parameters) { // 如果可用 command 字典用含有指定类型的 command if (ContainsCommands(type)) { ICommand command = null; // 从字典中获取指定类型的 command var item = commands[type]; // 如果是 ICommand 对象(这就代表对象是单例模式) if (item is ICommand) { command = (ICommand)item; } else { // 否则就是对象池模式 command = GetCommandFromPool(type, (List <ICommand>)item); container.Inject(command); } // 设置 command.dispatcher 为当前,将 command.running 设为真,再将参数 parameters 传人并执行 command.dispatcher = this; command.running = true; command.Execute(parameters); // 如果 command.keepAlive 为真 if (command.keepAlive) { //如果命令实现了 IUpdatable 接口,并且 EventContainerAOT 的 IUpdatable list 中还没有添加该 command 就进行添加 if (command is IUpdatable && !EventContainerAOT.updateable.Contains((IUpdatable)command)) { EventContainerAOT.updateable.Add((IUpdatable)command); } } else { // 不为真则释放 command Release(command); } } // 如果不含有该 command 就抛出异常 else { throw new Exceptions( string.Format(Exceptions.NO_COMMAND_FOR_TYPE, type)); } }
/// <summary> /// 为对象注入 /// </summary> /// <param name="obj">被注入的对象</param> public static void Inject(object obj) { mContainer.Inject(obj); }