Exemple #1
0
        public static ConstructorDeclarationSyntax AddStaticModifier(ConstructorDeclarationSyntax constructorDeclaration)
        {
            SyntaxTokenList modifiers = constructorDeclaration.Modifiers;

            if (!modifiers.Contains(SyntaxKind.StaticKeyword))
            {
                SyntaxTokenList newModifiers = modifiers;

                if (modifiers.ContainsAccessModifier())
                {
                    newModifiers = modifiers.RemoveAccessModifiers();

                    if (newModifiers.Any())
                    {
                        newModifiers = newModifiers.ReplaceAt(0, newModifiers[0].WithLeadingTrivia(modifiers[0].LeadingTrivia));
                        newModifiers = Inserter.InsertModifier(newModifiers, SyntaxKind.StaticKeyword);
                    }
                    else
                    {
                        newModifiers = Inserter.InsertModifier(newModifiers, CSharpFactory.StaticKeyword().WithLeadingTrivia(modifiers[0].LeadingTrivia));
                    }
                }
                else
                {
                    newModifiers = Inserter.InsertModifier(newModifiers, SyntaxKind.StaticKeyword);
                }

                return(constructorDeclaration.WithModifiers(newModifiers));
            }
            else
            {
                return(constructorDeclaration);
            }
        }
Exemple #2
0
        public static ConstructorDeclarationSyntax AddStaticModifier(ConstructorDeclarationSyntax constructorDeclaration)
        {
            if (constructorDeclaration == null)
            {
                throw new ArgumentNullException(nameof(constructorDeclaration));
            }

            SyntaxTokenList modifiers = constructorDeclaration.Modifiers;

            if (!modifiers.Contains(SyntaxKind.StaticKeyword))
            {
                SyntaxTokenList newModifiers = modifiers;

                if (modifiers.ContainsAccessModifier())
                {
                    newModifiers = modifiers.RemoveAccessModifiers();

                    if (newModifiers.Any())
                    {
                        newModifiers = newModifiers.ReplaceAt(0, newModifiers[0].WithLeadingTrivia(modifiers[0].LeadingTrivia));
                        newModifiers = newModifiers.InsertModifier(SyntaxKind.StaticKeyword, ModifierComparer.Instance);
                    }
                    else
                    {
                        newModifiers = newModifiers.InsertModifier(CSharpFactory.StaticKeyword().WithLeadingTrivia(modifiers[0].LeadingTrivia), ModifierComparer.Instance);
                    }
                }
                else
                {
                    newModifiers = newModifiers.InsertModifier(SyntaxKind.StaticKeyword, ModifierComparer.Instance);
                }

                return(constructorDeclaration.WithModifiers(newModifiers));
            }
            else
            {
                return(constructorDeclaration);
            }
        }