public static void Load(this MessageDispatcherComponent self) { self.Handlers.Clear(); HashSet <Type> types = Game.EventSystem.GetTypes(typeof(MessageHandlerAttribute)); foreach (Type type in types) { IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler; if (iMHandler == null) { Log.Error($"message handle {type.Name} 需要继承 IMHandler"); continue; } Type messageType = iMHandler.GetMessageType(); ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(messageType); if (opcode == 0) { Log.Error($"消息opcode为0: {messageType.Name}"); continue; } self.RegisterHandler(opcode, iMHandler); } }
private static void Load(this MessageDispatcherComponent self) { self.Handlers.Clear(); HashSet <Type> types = Game.EventSystem.GetTypes(typeof(MessageHandlerAttribute)); foreach (Type type in types) { IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler; if (iMHandler == null) { Log.Error($"message handle {type.Name} 需要继承 IMHandler"); continue; } object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false); foreach (object attr in attrs) { MessageHandlerAttribute messageHandlerAttribute = attr as MessageHandlerAttribute; Type messageType = iMHandler.GetMessageType(); ushort opcode = OpcodeTypeComponent.Instance.GetOpcode(messageType); if (opcode == 0) { Log.Error($"消息opcode为0: {messageType.Name}"); continue; } MessageDispatcherInfo messageDispatcherInfo = new (messageHandlerAttribute.SceneType, iMHandler); self.RegisterHandler(opcode, messageDispatcherInfo); } } }