private MyBuffer MessageToBuffer(IServerMessage message) { if (!(message.GetType().GetCustomAttribute(typeof(ServerMessageTypeAttribute)) is ServerMessageTypeAttribute messageTypeAttr)) { throw new NotImplementedException($"Not found message type for{message.GetType().Name}"); } var mesageType = messageTypeAttr.ServerMessageType; var buff = MyBuffer.Create() .SetUint8((byte)mesageType) .SetData(message); return(buff); }
/// <summary> /// Trigger event handlers for an incoming message /// </summary> /// <param name="message">Incoming message from IRC server</param> /// <remarks>Automatically determines types using reflection</remarks> public void Trigger(IServerMessage message) { if (message == null) { return; } var messageType = message.GetType(); var getHandlerMethod = typeof(EventHub).GetMethod(nameof(GetHandler), BindingFlags.NonPublic | BindingFlags.Instance).MakeGenericMethod(messageType); var handler = getHandlerMethod.Invoke(this, null); var genericArgsType = typeof(IrcMessageEventArgs <>).MakeGenericType(messageType); var args = Activator.CreateInstance(genericArgsType, message); var handlerType = typeof(ServerMessageEventHandler <>).MakeGenericType(messageType); var receivedEvent = handlerType.GetMethod("OnReceived", BindingFlags.NonPublic | BindingFlags.Instance); receivedEvent.Invoke(handler, new object[] { args }); }