예제 #1
0
        private bool TryGetArguments(JsonRpcRequest request, MethodSignature method, Span <object?> arguments)
        {
            Requires.NotNull(request, nameof(request));
            Requires.NotNull(method, nameof(method));
            Requires.Argument(arguments.Length == method.Parameters.Length, nameof(arguments), "Length must equal number of parameters in method signature.");

            // ref and out parameters aren't supported.
            if (method.HasOutOrRefParameters)
            {
                this.AddErrorMessage(string.Format(CultureInfo.CurrentCulture, Resources.MethodHasRefOrOutParameters, method));
                return(false);
            }

            // When there is a CancellationToken parameter, we require that it always be the last parameter.
            Span <ParameterInfo> methodParametersExcludingCancellationToken = Utilities.IsRunningOnMono
                ? method.Parameters.Take(method.TotalParamCountExcludingCancellationToken).ToArray()
                : method.Parameters.AsSpan(0, method.TotalParamCountExcludingCancellationToken);
            Span <object?> argumentsExcludingCancellationToken = arguments.Slice(0, method.TotalParamCountExcludingCancellationToken);

            if (method.HasCancellationTokenParameter)
            {
                arguments[arguments.Length - 1] = CancellationToken.None;
            }

            switch (request.TryGetTypedArguments(methodParametersExcludingCancellationToken, argumentsExcludingCancellationToken))
            {
            case JsonRpcRequest.ArgumentMatchResult.Success:
                return(true);

            case JsonRpcRequest.ArgumentMatchResult.ParameterArgumentCountMismatch:
                string methodParameterCount;
                methodParameterCount = string.Format(CultureInfo.CurrentCulture, "{0} - {1}", method.RequiredParamCount, method.TotalParamCountExcludingCancellationToken);
                this.AddErrorMessage(string.Format(
                                         CultureInfo.CurrentCulture,
                                         Resources.MethodParameterCountDoesNotMatch,
                                         method,
                                         methodParameterCount,
                                         request.ArgumentCount));
                return(false);

            case JsonRpcRequest.ArgumentMatchResult.ParameterArgumentTypeMismatch:
                this.AddErrorMessage(string.Format(CultureInfo.CurrentCulture, Resources.MethodParametersNotCompatible, method));
                return(false);

            case JsonRpcRequest.ArgumentMatchResult.MissingArgument:
                this.AddErrorMessage(Resources.RequiredArgumentMissing);
                return(false);

            default:
                return(false);
            }
        }