object[] ReplaceAllObservablesWithConnections(Type[] parameterTypes, object[] args, OperatorInfo operatorInfo)
        {
            object[] parameterValues = new object[args.Length];

            for (int i = 0; i < parameterValues.Length; i++)
            {
                object connectionObject;

                if (ConnectionFactory.TryCreateConnection(parameterTypes[i], args[i], operatorInfo, out connectionObject))
                {
                    parameterValues[i] = connectionObject;
                }
                else
                {
                    parameterValues[i] = args[i];
                }
            }

            return(parameterValues);
        }
        IMethodReturnMessage HandleRefCount(IMethodCallMessage call, System.Reflection.MethodInfo method, Type connectableOperatorConnectionType, Type signalType, OperatorInfo operatorInfo)
        {
            Debug.Assert(call.InArgs.Length == 1);

            var args = new object[] {
                Activator.CreateInstance(
                    connectableOperatorConnectionType,
                    new object[] { _session, call.InArgs[0], operatorInfo }
                    )
            };

            var actualObservable = MethodInvoker.Invoke(_queryLanguage, _queryLanguageType, method, call.InArgs);
            var ret = OperatorFactory.CreateOperatorObservable(actualObservable, signalType, operatorInfo);

            return(new ReturnMessage(ret, null, 0, null, call));
        }
        Func <IMethodCallMessage, IMethodReturnMessage> CreateRefCountHandler(IMethodCallMessage call, System.Reflection.MethodInfo method, OperatorInfo operatorInfo)
        {
            var signalType = method.GetGenericArguments()[0];
            var connectableOperatorConnectionType = typeof(ConnectableOperatorConnection <>).MakeGenericType(signalType);

            return(c => HandleRefCount(c, method, connectableOperatorConnectionType, signalType, operatorInfo));
        }
        private object ProduceActualObservable(IMethodCallMessage call, System.Reflection.MethodInfo method, OperatorInfo operatorInfo)
        {
            var args = ReplaceAllObservablesWithConnections(
                method.GetParameters().Select(p => p.ParameterType).ToArray(),
                call.InArgs,
                operatorInfo
                );

            return(MethodInvoker.Invoke(_queryLanguage, _queryLanguageType, method, args));
        }
        private IMethodReturnMessage HandleConnectableReturnType(IMethodCallMessage call, System.Reflection.MethodInfo method, OperatorInfo operatorInfo)
        {
            var genericType      = method.ReturnType.GetGenericArguments()[0];
            var actualObservable = ProduceActualObservable(call, method, operatorInfo);

            var connectable = Activator.CreateInstance(
                typeof(ConnectableOperatorObservable <>).MakeGenericType(genericType),
                new object[] { _session, actualObservable, operatorInfo }
                );

            return(new ReturnMessage(connectable, null, 0, null, call));
        }
        private IMethodReturnMessage HandleObservableReturnType(IMethodCallMessage call, System.Reflection.MethodInfo method, OperatorInfo operatorInfo)
        {
            var actualObservable = ProduceActualObservable(call, method, operatorInfo);

            var ret = OperatorFactory.CreateOperatorObservable(actualObservable, method.ReturnType.GetGenericArguments()[0], operatorInfo);

            return(new ReturnMessage(ret, null, 0, null, call));
        }