Esempio n. 1
0
        private static async Task <Document> CreateMappingLambda(Document document, InvocationExpressionSyntax invocation, CancellationToken cancellationToken)
        {
            var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

            var syntaxGenerator        = SyntaxGenerator.GetGenerator(document);
            var methodInvocationSymbol = semanticModel.GetSymbolInfo(invocation.Expression, cancellationToken);
            var mappingOverload        = methodInvocationSymbol.CandidateSymbols.OfType <IMethodSymbol>().FirstOrDefault(IsMappingMethod);

            if (mappingOverload == null)
            {
                return(document);
            }

            var parameterType     = ((INamedTypeSymbol)mappingOverload.Parameters[0].Type);
            var sourceElementType = parameterType.TypeArguments[0];
            var targetElementType = GetExpressionType(semanticModel, invocation);

            if (targetElementType == null)
            {
                return(document);
            }

            var mappingEngine         = new MappingEngine(semanticModel, syntaxGenerator);
            var sourceListElementType = new AnnotatedType(sourceElementType);
            var mappingLambda         = await mappingEngine.CreateMappingLambdaAsync("x", sourceListElementType, targetElementType, new MappingPath(), new MappingContext(invocation, semanticModel)).ConfigureAwait(false);

            return(await document.ReplaceNodes(invocation, invocation.WithArgumentList(SyntaxFactory.ArgumentList().AddArguments(SyntaxFactory.Argument((ExpressionSyntax)mappingLambda))), cancellationToken).ConfigureAwait(false));
        }