public ParameterListPair BuildParameters(
            Type sourceType,
            MethodInfo asyncMethod,
            MethodInfo syncMethod,
            UniqueIdentifierBuilder identifierBuilder)
        {
            Debug.Assert(sourceType is not null);
            Debug.Assert(asyncMethod is not null);
            Debug.Assert(syncMethod is not null);
            Debug.Assert(identifierBuilder is not null);

            var operationName   = OperationNameHelper.GetOperationName(asyncMethod);
            var asyncParameters = asyncMethod.GetParameters();
            var syncParameters  = syncMethod.GetParameters();

            Debug.Assert(asyncParameters.Length == syncParameters.Length ||
                         asyncParameters.Length == syncParameters.Length + 1 &&
                         asyncParameters[^ 1].ParameterType == typeof(CancellationToken));

            var asyncParametersBuilder = ImmutableList.CreateBuilder <Parameter>();
            var syncParametersBuilder  = ImmutableList.CreateBuilder <Parameter>();

            for (var i = 0; i < syncParameters.Length; i++)
            {
                var asyncParameter = asyncParameters[i];
                var syncParameter  = syncParameters[i];

                var(constructedAsyncParam, constructedSyncParam) = BuildParameter(
                    sourceType,
                    operationName,
                    asyncParameter,
                    syncParameter,
                    identifierBuilder);

                asyncParametersBuilder.Add(constructedAsyncParam);
                syncParametersBuilder.Add(constructedSyncParam);
            }

            if (asyncParameters.Length == syncParameters.Length + 1 &&
                asyncParameters[^ 1].ParameterType == typeof(CancellationToken))
            {
                asyncParametersBuilder.Add(Parameter.Default(asyncParameters[^ 1].Name !, async(writer, variableName) =>
        public ResolvedOptions ResolveOptions(MethodInfo method, ParameterList arguments)
        {
            var operationName = OperationNameHelper.GetOperationName(method);
            var parameters    = method.GetParameters().AsSpan();

            if (parameters[^ 1].ParameterType == typeof(CancellationToken))
Esempio n. 3
0
        private static MethodInfo?FindSyncQueryableMethod(MethodInfo method)
        {
            Type[]? asyncGenericArguments = null;
            var asyncParameters = method.GetParameters();
            var operationName   = OperationNameHelper.GetOperationName(method);

            var candidates = typeof(Queryable)
                             .GetMethods(BindingFlags.Public | BindingFlags.Static)
                             .Where(p => p.Name.Equals(operationName, StringComparison.Ordinal));

            foreach (var candidate in candidates)
            {
                // If the candidate is generic, construct the candidate with the async generic args
                var constructedCandidate = candidate;

                if (constructedCandidate.IsGenericMethodDefinition)
                {
                    if (!method.IsGenericMethod)
                    {
                        continue;
                    }

                    asyncGenericArguments ??= method.GetGenericArguments();

                    if (constructedCandidate.GetGenericArguments().Length != asyncGenericArguments.Length)
                    {
                        continue;
                    }

                    constructedCandidate = candidate.MakeGenericMethod(asyncGenericArguments);
                }
                else if (method.IsGenericMethod)
                {
                    continue;
                }

                var parameters    = constructedCandidate.GetParameters();
                var argumentMatch = true;

                if (parameters.Length != asyncParameters.Length)
                {
                    continue;
                }

                for (var j = 0; j < parameters.Length; j++)
                {
                    var asyncParameter = asyncParameters[j];
                    var parameter      = parameters[j];

                    if (!TypeTranslationHelper.IsEquivalentAsyncType(
                            parameter.ParameterType,
                            asyncParameter.ParameterType))
                    {
                        argumentMatch = false;
                        break;
                    }
                }

                if (!argumentMatch)
                {
                    continue;
                }

                if (!TypeTranslationHelper.IsEquivalentAsyncType(
                        constructedCandidate.ReturnType,
                        method.ReturnType))
                {
                    continue;
                }

                return(candidate);
            }

            return(null);
        }