Exemplo n.º 1
0
        /// <summary>
        /// Service method for AddEventHandler. Adds a method with a connect attribute as connect eventhandler.
        /// </summary>
        private void AddConnectHandlerWithAttribute(object handler, IEventFilter filter, MethodInfo method)
        {
            var methodParameters = method.GetParameters();

            if (methodParameters.Length != 1 || methodParameters[0].ParameterType != typeof(IObjectConnection))
            {
                return;
            }

            var exp = Expression.GetActionType(typeof(IObjectConnection));

            var delegat = method.CreateDelegate(exp, handler);

            typeof(IObjectConnection)
            .GetMethod("OnConnect")
            .Invoke(this, new object[] { delegat, filter?.GenerateFunc(method) ?? null, handler });
        }
        private void AddDisconnectHandler(object commandHandler, IEventFilter filter, MethodInfo method)
        {
            var parameters = method.GetParameters();

            if (parameters.Length == 1 && parameters[0].ParameterType == typeof(IObjectConnection))
            {
                var connectObj = new ConnectEventObject()
                {
                    Action     = method.CreateDelegate(typeof(Action <IObjectConnection>), commandHandler) as Action <IObjectConnection>,
                    CanExecute = filter?.GenerateFunc(method),
                };

                if (ConnectObject == null)
                {
                    DisconnectObject = connectObj;
                }
                else
                {
                    connectObj.Next  = DisconnectObject;
                    DisconnectObject = connectObj;
                }
            }
        }
        private void AddCommandHandler(object commandHandler, IEventFilter filter, MethodInfo method, MethodInfo deserializerMethod)
        {
            var methodParameters = method.GetParameters();

            if (methodParameters.Length != 2 || methodParameters[0].ParameterType != typeof(IObjectConnection))
            {
                return;
            }

            Type commandObjectType = methodParameters[1].ParameterType;

            var methodAction        = Expression.GetActionType(typeof(IObjectConnection), commandObjectType);
            var deserializeMethInfo = deserializerMethod.MakeGenericMethod(commandObjectType);
            var deserializeFunc     = Expression.GetFuncType(typeof(byte[]), commandObjectType);
            var commandObj          = new CommandEventObject
            {
                CommandMethod      = Delegate.CreateDelegate(methodAction, commandHandler, method),
                DeserializerMethod = Delegate.CreateDelegate(deserializeFunc, serializer, deserializeMethInfo),
                CanExecute         = filter?.GenerateFunc(method),
            };

            CommandObjects[commandObjectType.FullName] = commandObj;
        }