Пример #1
0
        /// <summary>
        /// Invokes the method with the specified parameters, returns the result of the method
        /// </summary>
        /// <exception cref="RpcInvalidParametersException">Thrown when conversion of parameters fails or when invoking the method is not compatible with the parameters</exception>
        /// <param name="parameters">List of parameters to invoke the method with</param>
        /// <returns>The result of the invoked method</returns>
        public object Invoke(params object[] parameters)
        {
            object obj = null;

            if (this.serviceProvider != null)
            {
                //Use service provider (if exists) to create instance
                var objectFactory = ActivatorUtilities.CreateFactory(this.type, new Type[0]);
                obj = objectFactory(this.serviceProvider, null);
            }
            if (obj == null)
            {
                //Use reflection to create instance if service provider failed or is null
                obj = Activator.CreateInstance(this.type);
            }
            try
            {
                parameters = this.ConvertParameters(parameters);

                object returnObj = this.methodInfo.Invoke(obj, parameters);

                returnObj = RpcMethod.HandleAsyncResponses(returnObj);

                return(returnObj);
            }
            catch (TargetInvocationException ex)
            {
                throw new RpcUnknownException("Unknown exception when running rpc method.", ex);
            }
            catch (Exception ex)
            {
                throw new RpcInvalidParametersException("Unable to call method. Most likely invalid parameters for method.", ex);
            }
        }
Пример #2
0
        /// <summary>
        /// Invokes the method with the specified parameters, returns the result of the method
        /// </summary>
        /// <exception cref="RpcInvalidParametersException">Thrown when conversion of parameters fails or when invoking the method is not compatible with the parameters</exception>
        /// <param name="parameters">List of parameters to invoke the method with</param>
        /// <returns>The result of the invoked method</returns>
        public async Task <object> InvokeAsync(params object[] parameters)
        {
            object obj = null;

            if (this.serviceProvider != null)
            {
                //Use service provider (if exists) to create instance
                var objectFactory = ActivatorUtilities.CreateFactory(this.type, new Type[0]);
                obj = objectFactory(this.serviceProvider, null);
            }
            if (obj == null)
            {
                //Use reflection to create instance if service provider failed or is null
                obj = Activator.CreateInstance(this.type);
            }
            try
            {
                parameters = this.ConvertParameters(parameters);

                object returnObj = this.methodInfo.Invoke(obj, parameters);

                returnObj = await RpcMethod.HandleAsyncResponses(returnObj);

                return(returnObj);
            }
            catch (TargetInvocationException ex)
            {
                throw new RpcUnknownException("Exception occurred from target method execution.", ex);
            }
            catch (Exception ex)
            {
                throw new RpcInvalidParametersException("Exception from attempting to invoke method. Possibly invalid parameters for method.", ex);
            }
        }
Пример #3
0
        /// <summary>
        /// Detects if list of parameters matches the method signature
        /// </summary>
        /// <param name="parameterList">Array of parameters for the method</param>
        /// <returns>True if the method signature matches the parameterList, otherwise False</returns>
        public bool HasParameterSignature(object[] parameterList)
        {
            if (parameterList == null)
            {
                throw new ArgumentNullException(nameof(parameterList));
            }
            if (parameterList.Count() > this.parameterInfoList.Count())
            {
                return(false);
            }

            for (int i = 0; i < this.parameterInfoList.Count(); i++)
            {
                ParameterInfo parameterInfo = this.parameterInfoList[i];
                if (parameterList.Count() <= i)
                {
                    if (!parameterInfo.IsOptional)
                    {
                        return(false);
                    }
                }
                else
                {
                    object parameter = parameterList[i];
                    bool   isMatch   = RpcMethod.ParameterMatches(parameterInfo, parameter);
                    if (!isMatch)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Пример #4
0
        /// <summary>
        /// Detects if list of parameters matches the method signature
        /// </summary>
        /// <param name="parameterList">Array of parameters for the method</param>
        /// <returns>True if the method signature matches the parameterList, otherwise False</returns>
        public bool HasParameterSignature(object[] parameterList, out object[] correctedParameterList)
        {
            if (parameterList == null)
            {
                throw new ArgumentNullException(nameof(parameterList));
            }

            correctedParameterList = parameterList;
            if (parameterList.Count() > this.parameterInfoList.Count())
            {
                return(false);
            }

            for (int i = 0; i < this.parameterInfoList.Count(); i++)
            {
                ParameterInfo parameterInfo = this.parameterInfoList[i];
                if (parameterList.Count() <= i)
                {
                    if (!parameterInfo.IsOptional)
                    {
                        return(false);
                    }
                    correctedParameterList = new object[correctedParameterList.Length + 1];
                    correctedParameterList[correctedParameterList.Length - 1] = Type.Missing;
                }
                else
                {
                    object parameter = parameterList[i];
                    bool   isMatch   = RpcMethod.ParameterMatches(parameterInfo, parameter);
                    if (!isMatch)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }