/// <summary> /// Registers all packet handlers defined in the given type. /// </summary> /// <param name = "type">the type to search through for packet handlers</param> public void Register(Type type) { if (type.IsAbstract || !(type.GetInterfaces().Contains(typeof(TContainer)) || type.IsSubclassOf(typeof(TContainer)))) { return; } var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public); TContainer handlerContainer; try { handlerContainer = (TContainer)Activator.CreateInstance(type, true); } catch (Exception e) { throw new Exception("Unable to create HandlerContainer " + type.Name + ".\n " + e.Message); } foreach (var method in methods) { var attributes = method.GetCustomAttributes(typeof(TAttribute), false) as TAttribute[]; if (attributes == null || attributes.Length == 0) { continue; } try { if ( method.GetParameters().Count( entry => (entry.ParameterType.IsSubclassOf(typeof(Message)) || (entry.ParameterType == typeof(TClient) || entry.ParameterType.IsSubclassOf(typeof(TClient))))) != 2) { throw new ArgumentException("Incorrect delegate parameters"); } var handlerDelegate = DynamicExtension.CreateDelegate(method, typeof(TClient), typeof(Message)) as Action <object, TClient, Message>; foreach (var attribute in attributes) { RegisterHandler(attribute.MessageId, handlerContainer, attribute, handlerDelegate); } } catch (Exception e) { var handlerStr = type.FullName + "." + method.Name; throw new Exception("Unable to register PacketHandler " + handlerStr + ".\n Make sure its arguments are: " + typeof(TClient).FullName + ", " + typeof(Message).FullName + ".\n" + e.Message); } } }
public static void Initialize(Assembly asm) { var methods = asm.GetTypes() .SelectMany(t => t.GetMethods()) .Where(m => m.GetCustomAttributes(typeof(HeaderPacketAttribute), false).Length > 0) .ToArray(); foreach (var method in methods) { var action = DynamicExtension.CreateDelegate(method, typeof(SimpleClient), typeof(NetworkMessage)) as Action <object, SimpleClient, NetworkMessage>; MethodHandlers.Add((HeaderEnum)method.CustomAttributes.ToArray()[0].ConstructorArguments[0].Value, action); } }
public static void RegisterAll() { Assembly asm = Assembly.GetAssembly(typeof(MessageInitializer)); foreach (Type type in asm.GetTypes().Where(w => w.IsSubclassOf(typeof(DiscordHandlerContener)))) { var methods = type.GetMethods().Where(x => x.CustomAttributes.Any(w => w.AttributeType == typeof(DiscordHandlerAttribute))); foreach (var method in methods) { var messageId = method.GetCustomAttribute <DiscordHandlerAttribute>().MessageId; var handlerDelegate = DynamicExtension.CreateDelegate(method, typeof(BaseMessage)) as Action <object, BaseMessage>; m_handlers.Add(messageId, handlerDelegate); } } }
private void InitializeHandlers() { foreach (var method in GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) { if (method.Name == "HandleMessage") { continue; } var parameters = method.GetParameters(); if (parameters.Length != 1 || !parameters[0].ParameterType.IsSubclassOf(typeof(IPCMessage))) { continue; } m_handlers.Add(parameters[0].ParameterType, (Action <object, IPCMessage>)DynamicExtension.CreateDelegate(method, typeof(IPCMessage))); } }