static SyntaxNode AddMissingProperties(SyntaxNode root, AnonymousObjectCreationExpressionSyntax anonymousObject, ITypeSymbol contractType)
        {
            var newRoot = root;

            List <IPropertySymbol> contractProperties = contractType.GetContractProperties();

            var propertiesToAdd = new List <AnonymousObjectMemberDeclaratorSyntax>();

            foreach (var messageContractProperty in contractProperties)
            {
                var initializer = anonymousObject.Initializers.FirstOrDefault(i => GetName(i) == messageContractProperty.Name);
                if (initializer == null)
                {
                    var path          = Enumerable.Empty <ITypeSymbol>();
                    var propertyToAdd = CreateProperty(messageContractProperty, path);
                    propertiesToAdd.Add(propertyToAdd);
                }
            }

            if (propertiesToAdd.Any())
            {
                var newAnonymousObject = anonymousObject
                                         .AddInitializers(propertiesToAdd.ToArray())
                                         .WithAdditionalAnnotations(Formatter.Annotation);
                newRoot = newRoot.ReplaceNode(anonymousObject, newAnonymousObject);
            }

            return(newRoot);
        }
Exemplo n.º 2
0
        public CSharpSyntaxNode Convert(ObjectLiteralExpression node)
        {
            List <Node> properties = node.Properties;
            Node        type       = node.Type;

            if (type.Kind == NodeKind.TypeLiteral && properties.Count >= 2 && (type as TypeLiteral).Members[0].Kind != NodeKind.IndexSignature)
            {
                return(SyntaxFactory.TupleExpression().AddArguments(properties.ToCsNodes <ArgumentSyntax>()));
            }
            else if (type.Kind == NodeKind.AnyKeyword)
            {
                AnonymousObjectCreationExpressionSyntax csAnonyNewExpr = SyntaxFactory.AnonymousObjectCreationExpression();
                foreach (PropertyAssignment prop in node.Properties)
                {
                    string           propName  = prop.Name.Text;
                    Node             initValue = prop.Initializer;
                    ExpressionSyntax valueExpr = initValue.ToCsNode <ExpressionSyntax>();

                    if (type.Kind == NodeKind.TypeLiteral && initValue.Kind == NodeKind.NullKeyword)
                    {
                        Node memType = TypeHelper.GetTypeLiteralMemberType(type as TypeLiteral, propName);
                        if (memType != null)
                        {
                            valueExpr = SyntaxFactory.CastExpression(memType.ToCsNode <TypeSyntax>(), valueExpr);
                        }
                    }

                    csAnonyNewExpr = csAnonyNewExpr.AddInitializers(SyntaxFactory.AnonymousObjectMemberDeclarator(
                                                                        SyntaxFactory.NameEquals(propName),
                                                                        valueExpr));
                }
                return(csAnonyNewExpr);
            }
            else
            {
                ObjectCreationExpressionSyntax csObjLiteral  = SyntaxFactory.ObjectCreationExpression(type.ToCsNode <TypeSyntax>()).AddArgumentListArguments();
                List <ExpressionSyntax>        initItemExprs = new List <ExpressionSyntax>();
                foreach (PropertyAssignment prop in properties)
                {
                    ExpressionSyntax csNameExpression = SyntaxFactory.LiteralExpression(
                        SyntaxKind.StringLiteralExpression,
                        SyntaxFactory.Literal(prop.Name.Text));
                    InitializerExpressionSyntax itemInitExpr = SyntaxFactory
                                                               .InitializerExpression(SyntaxKind.ComplexElementInitializerExpression)
                                                               .AddExpressions(csNameExpression, prop.Initializer.ToCsNode <ExpressionSyntax>());

                    initItemExprs.Add(itemInitExpr);
                }
                if (initItemExprs.Count > 0)
                {
                    return(csObjLiteral.WithInitializer(SyntaxFactory.InitializerExpression(
                                                            SyntaxKind.CollectionInitializerExpression,
                                                            SyntaxFactory.SeparatedList(initItemExprs))));
                }
                return(csObjLiteral);
            }
        }