private void AddHandlerForParameterizedAction(RouteHandler routes, HttpMethodAttribute att, MethodInfo info) { ParameterizedAction action = ParameterizedActionFactory.CreateAction(info); ParameterizedActionTarget target = new ParameterizedActionTarget(this, info, action); AddImplicitRouteHandler(target, att.Patterns, att.Methods); }
private void AddDefaultHandlerForParameterizedAction(RouteHandler routes, MethodInfo info) { ParameterizedAction action = ParameterizedActionFactory.CreateAction(info); ParameterizedActionTarget target = new ParameterizedActionTarget(this, info, action); AddImplicitRouteHandler(target, new string [] { "/" + info.Name }, HttpMethods.RouteMethods); }
public ParameterizedActionTarget(object target, MethodInfo method, ParameterizedAction action) { if (method == null) throw new ArgumentNullException ("method"); if (action == null) throw new ArgumentNullException ("action"); this.target = target; Action = action; parameters = method.GetParameters (); }
/// <summary> /// Registers a listener parameterized action for the given event type /// </summary> /// <param name="action">Action to register</param> /// <typeparam name="T">Event type</typeparam> public static void RegisterListener <T>(ParameterizedAction <T> action) { Type type = typeof(T); if (ParameterizedSubscribers.TryGetValue(type, out var currentDelegate)) { ParameterizedSubscribers[type] = Delegate.Combine(currentDelegate, action); } else { ParameterizedSubscribers.Add(type, action); } }
/// <summary> /// Unregisters the listener parameterized action for the given event type /// </summary> /// <param name="action">Action to unregister</param> /// <typeparam name="T">Event type</typeparam> public static void UnregisterListener <T>(ParameterizedAction <T> action) { Type type = typeof(T); if (ParameterizedSubscribers.TryGetValue(type, out var currentDelegate)) { ParameterizedSubscribers[type] = Delegate.Remove(currentDelegate, action); if (ParameterizedSubscribers[type] == null) { ParameterizedSubscribers.Remove(type); } } }
public ParameterizedActionTarget(object target, MethodInfo method, ParameterizedAction action) { if (method == null) { throw new ArgumentNullException("method"); } if (action == null) { throw new ArgumentNullException("action"); } this.target = target; Action = action; parameters = method.GetParameters(); }
public static ParameterizedAction CreateAction(MethodInfo method) { DynamicMethod dynamic = new DynamicMethod(String.Empty, typeof(void), new Type [] { typeof(object), typeof(object[]) }, method.DeclaringType.Module); ILGenerator codegen = dynamic.GetILGenerator(); ParameterInfo [] parameters = method.GetParameters(); Type [] param_types = new Type [parameters.Length]; for (int i = 0; i < param_types.Length; i++) { if (parameters [i].ParameterType.IsByRef) { Console.Error.WriteLine("By Ref parameters are not allowed in Action signatures."); return(null); } param_types [i] = parameters [i].ParameterType; } LocalBuilder [] locals = new LocalBuilder [param_types.Length]; for (int i = 0; i < param_types.Length; i++) { locals [i] = codegen.DeclareLocal(param_types [i], true); } for (int i = 0; i < param_types.Length; i++) { codegen.Emit(OpCodes.Ldarg_1); EmitFastInt(codegen, i); codegen.Emit(OpCodes.Ldelem_Ref); EmitCastToReference(codegen, param_types [i]); codegen.Emit(OpCodes.Stloc, locals [i]); } if (!method.IsStatic) { codegen.Emit(OpCodes.Ldarg_0); } for (int i = 0; i < param_types.Length; i++) { codegen.Emit(OpCodes.Ldloc, locals [i]); } if (method.IsStatic) { codegen.EmitCall(OpCodes.Call, method, null); } else { codegen.EmitCall(OpCodes.Callvirt, method, null); } codegen.Emit(OpCodes.Ret); ParameterizedAction action = (ParameterizedAction)dynamic.CreateDelegate(typeof(ParameterizedAction)); return(action); }