private static IEnumerable <SyntaxNode> GenerateMappingCode(IMethodSymbol methodSymbol, SyntaxGenerator generator, MappingGenerator mappingGenerator, SemanticModel semanticModel) { if (SymbolHelper.IsPureMappingFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ReturnType; return(mappingGenerator.MapTypes(source.Type, targetType, generator.IdentifierName(source.Name))); } var isMappingConstructor = SymbolHelper.IsMappingConstructor(methodSymbol); if (SymbolHelper.IsUpdateThisObjectFunction(methodSymbol) || isMappingConstructor) { var source = methodSymbol.Parameters[0]; var targetType = methodSymbol.ContainingType; return(mappingGenerator.MapTypes(source.Type, targetType, generator.IdentifierName(source.Name), generator.ThisExpression(), targetExists: true, isConstructorContext: isMappingConstructor)); } if (SymbolHelper.IsUpdateParameterFunction(methodSymbol)) { var source = methodSymbol.Parameters[0]; var target = methodSymbol.Parameters[1]; return(mappingGenerator.MapTypes(source.Type, target.Type, generator.IdentifierName(source.Name), generator.IdentifierName(target.Name), targetExists: true)); } var isMultiParameterConstructor = SymbolHelper.IsMultiParameterUpdateThisObjectFunction(methodSymbol); if (isMultiParameterConstructor || SymbolHelper.IsMultiParameterMappingConstructor(methodSymbol)) { var sourceFinder = new LocalScopeMappingSourceFinder(semanticModel, methodSymbol.Parameters); return(ObjectHelper.GetPublicPropertySymbols(methodSymbol.ContainingType) .Where(property => property.SetMethod != null || (property.CanBeSetOnlyFromConstructor() && isMultiParameterConstructor)) .Select(property => new { source = sourceFinder.FindMappingSource(property.Name, property.Type), target = new MappingElement() { Expression = SyntaxFactory.IdentifierName(property.Name), ExpressionType = property.Type } }) .Where(x => x.source != null) .SelectMany(pair => mappingGenerator.Map(pair.source, pair.target))); } return(Enumerable.Empty <SyntaxNode>()); }
private async Task <Document> InitizalizeWithLocals(Document document, InitializerExpressionSyntax objectInitializer, CancellationToken cancellationToken) { var semanticModel = await document.GetSemanticModelAsync(cancellationToken); var objectCreationExpression = objectInitializer.FindContainer <ObjectCreationExpressionSyntax>(); var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, objectCreationExpression).Type; var mappingSourceFinder = new LocalScopeMappingSourceFinder(semanticModel, objectInitializer); var propertiesToSet = ObjectHelper.GetPublicPropertySymbols(createdObjectType).Where(x => x.SetMethod?.DeclaredAccessibility == Accessibility.Public); var initExpressions = propertiesToSet.Aggregate(objectInitializer.Expressions, (expr, property) => { var mappingSource = mappingSourceFinder.FindMappingSource(property.Name, property.Type); if (mappingSource != null) { var assignmentExpression = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(property.Name), mappingSource.Expression); return(expr.Add(assignmentExpression)); } return(expr); }); return(await document.ReplaceNodes(objectInitializer, objectInitializer.WithExpressions(initExpressions), cancellationToken)); }