コード例 #1
0
        private ExpressionSyntax BuildXmlnsAttributeName(IdentifierNameSyntax fieldIdentifierName)
        {
            var xmlns = SyntaxFactory.MemberAccessExpression(CSSyntaxKind.SimpleMemberAccessExpression, SyntaxFactory.IdentifierName("XNamespace"), SyntaxFactory.IdentifierName("Xmlns"));

            return(SyntaxFactory.BinaryExpression(CSSyntaxKind.AddExpression, xmlns, CommonConversions.Literal(fieldIdentifierName.Identifier.ValueText)));
        }
コード例 #2
0
        public ClassDeclarationSyntax GenerateHelper()
        {
            if (!HasImports)
            {
                return(null);
            }

            var xAttributeList = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.List <AttributeListSyntax>(),
                SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.PrivateKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                CommonConversions.CreateVariableDeclarationAndAssignment(
                    "namespaceAttributes", SyntaxFactory.InitializerExpression(
                        CSSyntaxKind.ArrayInitializerExpression,
                        SyntaxFactory.SeparatedList <ExpressionSyntax>(
                            from x in _xNamespaceFields
                            let fieldIdentifierName = SyntaxFactory.IdentifierName(x.Declaration.Variables.Single().Identifier)
                                                      let namespaceNameExpression = SyntaxFactory.MemberAccessExpression(CSSyntaxKind.SimpleMemberAccessExpression, fieldIdentifierName, SyntaxFactory.IdentifierName("NamespaceName"))
                                                                                    let attributeNameExpression = fieldIdentifierName.IsEquivalentTo(DefaultIdentifierName) ? CommonConversions.Literal("xmlns") : BuildXmlnsAttributeName(fieldIdentifierName)
                                                                                                                  let arguments = SyntaxFactory.Argument(attributeNameExpression).Yield().Concat(SyntaxFactory.Argument(namespaceNameExpression))
                                                                                                                                  select SyntaxFactory.ObjectCreationExpression(SyntaxFactory.IdentifierName("XAttribute")).WithArgumentList(SyntaxFactory.ArgumentList(SyntaxFactory.SeparatedList(arguments))))),
                    SyntaxFactory.ArrayType(SyntaxFactory.IdentifierName("XAttribute"), SyntaxFactory.SingletonList(SyntaxFactory.ArrayRankSpecifier(SyntaxFactory.SingletonSeparatedList <ExpressionSyntax>(SyntaxFactory.OmittedArraySizeExpression()))))));


            var boilerplate = SyntaxFactory.ParseStatement(@"
                TContainer Apply<TContainer>(TContainer x) where TContainer : XContainer
                {
                    foreach (var d in x.Descendants()) {
                        foreach (var n in namespaceAttributes) {
                            var a = d.Attribute(n.Name);
                            if (a != null && a.Value == n.Value) {
                                a.Remove();
                            }
                        }
                    }
                    x.Add(namespaceAttributes);
                    return x;
                }") as LocalFunctionStatementSyntax;

            var applyMethod = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.List <AttributeListSyntax>(),
                SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                boilerplate.ReturnType,
                null,
                boilerplate.Identifier,
                boilerplate.TypeParameterList,
                boilerplate.ParameterList,
                boilerplate.ConstraintClauses,
                boilerplate.Body,
                boilerplate.ExpressionBody);

            return(SyntaxFactory.ClassDeclaration(
                       SyntaxFactory.List <AttributeListSyntax>(),
                       SyntaxFactory.TokenList(SyntaxFactory.Token(CSSyntaxKind.InternalKeyword), SyntaxFactory.Token(CSSyntaxKind.StaticKeyword)),
                       HelperClassUniqueIdentifierName.Identifier,
                       null, null,
                       SyntaxFactory.List <TypeParameterConstraintClauseSyntax>(),
                       SyntaxFactory.List(_xNamespaceFields.Concat <MemberDeclarationSyntax>(xAttributeList).Concat(applyMethod))
                       ));
        }
コード例 #3
0
        private async Task <SyntaxList <StatementSyntax> > AddLocalVariablesAsync(VBasic.VisualBasicSyntaxNode node, VBasic.SyntaxKind exitableType = default, bool isBreakableInCs = false)
        {
            _additionalLocals.PushScope(exitableType, isBreakableInCs);
            try {
                var convertedStatements = await _wrappedVisitor.Visit(node);

                var withLocals = await _additionalLocals.CreateLocalsAsync(node, convertedStatements, _generatedNames, _semanticModel);

                var allStatements = _additionalLocals.CreateStatements(node, withLocals, _generatedNames, _semanticModel);

                if (isBreakableInCs && exitableType == VBasic.SyntaxKind.TryKeyword)
                {
                    var doOnce = SyntaxFactory.DoStatement(SyntaxFactory.Block(allStatements), CommonConversions.Literal(false));
                    allStatements = SyntaxFactory.SingletonList <StatementSyntax>(doOnce);
                }
                return(allStatements);
            } finally {
                _additionalLocals.PopScope();
            }
        }