Beispiel #1
0
        public async Task <IReadOnlyList <AssignmentExpressionSyntax> > MapUsingSimpleAssignment(IReadOnlyCollection <IObjectField> targets,
                                                                                                 IMappingMatcher mappingMatcher,
                                                                                                 MappingContext mappingContext,
                                                                                                 MappingPath mappingPath = null, SyntaxNode globalTargetAccessor = null)
        {
            if (mappingPath == null)
            {
                mappingPath = new MappingPath();
            }

            var results = new List <AssignmentExpressionSyntax>();

            foreach (var match in await mappingMatcher.MatchAll(targets, syntaxGenerator, mappingContext, globalTargetAccessor).ConfigureAwait(false))
            {
                var sourceMappingElement = await MapExpression(match.Source, match.Target.ExpressionType, mappingContext, mappingPath.Clone()).ConfigureAwait(false);

                var sourceExpression = sourceMappingElement.Expression;
                if (sourceMappingElement.ExpressionType != match.Target.ExpressionType)
                {
                    mappingContext.AddMissingConversion(sourceMappingElement.ExpressionType.Type, match.Target.ExpressionType.Type);
                    if (mappingContext.WrapInCustomConversion)
                    {
                        var customConversionMethodName = syntaxGenerator.IdentifierName($"MapFrom{sourceMappingElement.ExpressionType.Type.Name}To{match.Target.ExpressionType.Type.Name}");
                        sourceExpression = (ExpressionSyntax)syntaxGenerator.InvocationExpression(customConversionMethodName, sourceExpression);
                    }
                }

                var assignmentExpression = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, match.Target.Expression, sourceExpression);
                results.Add(assignmentExpression);
            }

            return(results);
        }
        public IEnumerable <AssignmentExpressionSyntax> MapUsingSimpleAssignment(IReadOnlyCollection <IObjectField> targets,
                                                                                 IMappingMatcher mappingMatcher,
                                                                                 MappingContext mappingContext,
                                                                                 MappingPath mappingPath = null, SyntaxNode globalTargetAccessor = null)
        {
            if (mappingPath == null)
            {
                mappingPath = new MappingPath();
            }

            return(mappingMatcher.MatchAll(targets, syntaxGenerator, mappingContext, globalTargetAccessor)
                   .Select(match =>
            {
                var sourceMappingElement = MapExpression(match.Source, match.Target.ExpressionType, mappingContext, mappingPath.Clone());
                var sourceExpression = sourceMappingElement.Expression;
                if (sourceMappingElement.ExpressionType != match.Target.ExpressionType)
                {
                    mappingContext.AddMissingConversion(sourceMappingElement.ExpressionType.Type, match.Target.ExpressionType.Type);
                    if (mappingContext.WrapInCustomConversion)
                    {
                        var customConversionMethodName = syntaxGenerator.IdentifierName($"MapFrom{sourceMappingElement.ExpressionType.Type.Name}To{match.Target.ExpressionType.Type.Name}");
                        sourceExpression = (ExpressionSyntax)syntaxGenerator.InvocationExpression(customConversionMethodName, sourceExpression);
                    }
                }

                return AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, match.Target.Expression, sourceExpression);
            }).ToList());
        }