private static async Task <Document> ReplaceEmptyInitializationBlock(Document document,
                                                                             InitializerExpressionSyntax objectInitializer, SemanticModel semanticModel,
                                                                             IMappingSourceFinder mappingSourceFinder, CancellationToken cancellationToken)
        {
            var oldObjCreation    = objectInitializer.FindContainer <ObjectCreationExpressionSyntax>();
            var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, oldObjCreation).Type;
            var mappingEngine     = await MappingEngine.Create(document, cancellationToken, semanticModel.FindContextAssembly(objectInitializer));

            var newObjectCreation = mappingEngine.AddInitializerWithMapping(oldObjCreation, mappingSourceFinder, createdObjectType);

            return(await document.ReplaceNodes(oldObjCreation, newObjectCreation, cancellationToken));
        }
        private static async Task <Document> ReplaceEmptyInitializationBlock(Document document,
                                                                             InitializerExpressionSyntax objectInitializer, SemanticModel semanticModel,
                                                                             IMappingMatcher mappingMatcher, CancellationToken cancellationToken)
        {
            var oldObjCreation    = objectInitializer.FindContainer <ObjectCreationExpressionSyntax>();
            var createdObjectType = ModelExtensions.GetTypeInfo(semanticModel, oldObjCreation).Type;
            var mappingEngine     = await MappingEngine.Create(document, cancellationToken).ConfigureAwait(false);

            var mappingContext    = new MappingContext(objectInitializer, semanticModel);
            var newObjectCreation = await mappingEngine.AddInitializerWithMappingAsync(oldObjCreation, mappingMatcher, createdObjectType, mappingContext).ConfigureAwait(false);

            return(await document.ReplaceNodes(oldObjCreation, newObjectCreation, cancellationToken).ConfigureAwait(false));
        }
Exemple #3
0
        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));
        }
        private static IEnumerable <IMappingSourceFinder> GetAllPossibleSourceFinders(InitializerExpressionSyntax objectInitializer, SemanticModel semanticModel, SyntaxGenerator syntaxGenerator)
        {
            var localSymbols = semanticModel.GetLocalSymbols(objectInitializer);

            var queryExpression = objectInitializer.FindContainer <QueryExpressionSyntax>();

            if (queryExpression != null)
            {
                yield return(GetMappingSourceFindersForQueryExpression(semanticModel, syntaxGenerator, queryExpression, localSymbols));

                yield break;
            }

            yield return(new LocalScopeMappingSourceFinder(semanticModel, localSymbols));

            foreach (var localSymbol in localSymbols)
            {
                var symbolType = semanticModel.GetTypeForSymbol(localSymbol);
                if (symbolType != null && ObjectHelper.IsSimpleType(symbolType) == false)
                {
                    yield return(new ObjectMembersMappingSourceFinder(symbolType, SyntaxFactory.IdentifierName(localSymbol.Name), syntaxGenerator));
                }
            }
        }