当前位置: 首页>>代码示例>>C#>>正文


C# ClassDeclarationSyntax.WithMembers方法代码示例

本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.WithMembers方法的典型用法代码示例。如果您正苦于以下问题:C# ClassDeclarationSyntax.WithMembers方法的具体用法?C# ClassDeclarationSyntax.WithMembers怎么用?C# ClassDeclarationSyntax.WithMembers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax的用法示例。


在下文中一共展示了ClassDeclarationSyntax.WithMembers方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: VisitClassDeclaration

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            node = (ClassDeclarationSyntax)base.VisitClassDeclaration(node);
            SyntaxList<MemberDeclarationSyntax> newMembers = new SyntaxList<MemberDeclarationSyntax>();
            foreach (MemberDeclarationSyntax member in node.Members)
            {
                if (member.CSharpKind() == SyntaxKind.PropertyDeclaration)
                {
                    PropertyDeclarationSyntax prop = (PropertyDeclarationSyntax)member;
                    SyntaxList<AccessorDeclarationSyntax> newAccessors = new SyntaxList<AccessorDeclarationSyntax>();
                    bool implementfield = false;
                    foreach (AccessorDeclarationSyntax accessor in prop.AccessorList.Accessors)
                    {
                        if (accessor.Body == null)
                        {
                            switch (accessor.CSharpKind())
                            {
                                case SyntaxKind.GetAccessorDeclaration:
                                    implementfield = true;
                                    newAccessors = newAccessors.Add(accessor.WithBody(SyntaxFactory.Block(SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_" + prop.Identifier.ValueText)))));
                                    break;
                                case SyntaxKind.SetAccessorDeclaration:
                                    implementfield = true;
                                    newAccessors = newAccessors.Add(accessor.WithBody(SyntaxFactory.Block(SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName("_" + prop.Identifier.ValueText), SyntaxFactory.IdentifierName("value"))))));
                                    break;
                                default:
                                    newAccessors = newAccessors.Add(accessor);
                                    break;
                            }
                        }
                        else
                        {
                            newAccessors = newAccessors.Add(accessor);
                        }
                    }
                    if (implementfield)
                    {
                        SeparatedSyntaxList<VariableDeclaratorSyntax> variables = new SeparatedSyntaxList<VariableDeclaratorSyntax>();
                        variables = variables.Add(SyntaxFactory.VariableDeclarator("_" + prop.Identifier.ValueText));
                        newMembers = newMembers.Add(SyntaxFactory.FieldDeclaration(SyntaxFactory.VariableDeclaration(prop.Type, variables)).WithModifiers(prop.Modifiers));
                    }

                    newMembers = newMembers.Add(prop.WithAccessorList(prop.AccessorList.WithAccessors(newAccessors)));
                }
                else
                {
                    newMembers = newMembers.Add(member);
                }
            }
            return node.WithMembers(newMembers);
        }
开发者ID:LaxLacks,项目名称:ls2csc,代码行数:51,代码来源:AutoImplementedPropertyRewriter.cs

示例2: VisitClassDeclaration

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            var emptyConstructor = SyntaxFactory.ConstructorDeclaration(node.Identifier.Text)
                .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)))
                .WithBody(SyntaxFactory.Block());

            var insertIdx = 0;
            if (node.Members.Any(p => p is FieldDeclarationSyntax))
            {
                insertIdx = node.Members.LastIndexOf(p => p is FieldDeclarationSyntax) + 1;
            }

            node = node.WithMembers(node.Members.Insert(insertIdx, emptyConstructor));

            return base.VisitClassDeclaration(node);
        }
开发者ID:yohney,项目名称:common-refactorings-plugin,代码行数:16,代码来源:AddNewConstructorRewriter.cs

示例3: VisitClassDeclaration

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            //int indexers = node.Members.Count((m) => (m is IndexerDeclarationSyntax && !((IndexerDeclarationSyntax)m)));
            SyntaxList<MemberDeclarationSyntax> members = SyntaxFactory.List<MemberDeclarationSyntax>();
            int numIndexers = 0;
            foreach (MemberDeclarationSyntax member in node.Members)
            {
                if (member.CSharpKind() == SyntaxKind.IndexerDeclaration)
                {
                    IndexerDeclarationSyntax ids = (IndexerDeclarationSyntax)member;
                    numIndexers++;

                    foreach (AccessorDeclarationSyntax ads in ids.AccessorList.Accessors)
                    {
                        if (ads.Keyword.ToString() == "set")
                        {
                            members = members.Add(
                                SyntaxFactory.MethodDeclaration(ads.AttributeLists, ads.Modifiers.AddRange(ids.Modifiers), SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), ids.ExplicitInterfaceSpecifier, SyntaxFactory.Identifier(ads.Keyword.ToString() + "_Item"), null, SyntaxFactory.ParameterList(ids.ParameterList.Parameters), SyntaxFactory.List<TypeParameterConstraintClauseSyntax>(), ads.Body)
                                );
                        }
                        else
                        {
                            members = members.Add(
                                SyntaxFactory.MethodDeclaration(ads.AttributeLists, ads.Modifiers.AddRange(ids.Modifiers), ids.Type, ids.ExplicitInterfaceSpecifier, SyntaxFactory.Identifier(ads.Keyword.ToString() + "_Item"), null, SyntaxFactory.ParameterList(ids.ParameterList.Parameters), SyntaxFactory.List<TypeParameterConstraintClauseSyntax>(), ads.Body)
                                );
                        }
                    }
                }
                else
                {
                    members = members.Add(member);
                }
            }

            if (numIndexers>0)
            {
                return node.WithMembers(members);
            }

            return base.VisitClassDeclaration(node);
        }
开发者ID:LaxLacks,项目名称:ls2csc,代码行数:41,代码来源:IndexerRewriter.cs

示例4: VisitClassDeclaration

        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            var newName = "_" + this._relatedParam.Identifier.Text;
            var newField = SyntaxFactory.FieldDeclaration(
                    SyntaxFactory.VariableDeclaration(
                        this._relatedParam.Type,
                        SyntaxFactory.SingletonSeparatedList<VariableDeclaratorSyntax>(
                            SyntaxFactory.VariableDeclarator(newName))))
                .WithModifiers(SyntaxFactory.TokenList().Add(SyntaxFactory.Token(SyntaxKind.PrivateKeyword)));

            this.GeneratedField = newField;

            var insertIdx = 0;
            if (node.Members.Any(p => p is FieldDeclarationSyntax))
            {
                insertIdx = node.Members.LastIndexOf(p => p is FieldDeclarationSyntax) + 1;
            }

            node = node.WithMembers(node.Members.Insert(insertIdx, newField));

            return base.VisitClassDeclaration(node);
        }
开发者ID:yohney,项目名称:common-refactorings-plugin,代码行数:22,代码来源:IntroduceFieldRewriter.cs

示例5: VisitClassDeclaration

 public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) {
   if (node == this.type) {
     return node.WithMembers(node.Members.Add(this.pre));
   }
   return base.VisitClassDeclaration(node);
 }
开发者ID:asvishnyakov,项目名称:CodeContracts,代码行数:6,代码来源:RefactoringProvider.cs

示例6: SortMembers

 private static ClassDeclarationSyntax SortMembers(ClassDeclarationSyntax type)
 {
     var innerMembers = type.Members.ToList();
     innerMembers.Sort(StyleCop.Sort);
     type = type.WithMembers(SyntaxFactory.List(innerMembers));
     return type;
 }
开发者ID:azeno,项目名称:ImmutableObjectGraph,代码行数:7,代码来源:CodeGen+StyleCopCompliance.cs

示例7: VisitClassDeclaration


//.........这里部分代码省略.........
            node = (ClassDeclarationSyntax)base.VisitClassDeclaration(node);
            MethodDeclarationSyntax prector = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), ".prector");
            MethodDeclarationSyntax precctor = SyntaxFactory.MethodDeclaration(SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.VoidKeyword)), ".precctor");
            precctor = precctor.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));

            List<StatementSyntax> Initializers = new List<StatementSyntax>();
            List<StatementSyntax> StaticInitializers = new List<StatementSyntax>();
            foreach (MemberDeclarationSyntax member in node.Members)
            {
                if (member.CSharpKind() == SyntaxKind.FieldDeclaration)
                {
                    FieldDeclarationSyntax fds = (FieldDeclarationSyntax)member;

                    foreach (VariableDeclaratorSyntax vds in fds.Declaration.Variables)
                    {
                        if (vds.Initializer != null)
                        {
                            if (fds.Modifiers.ToString().Contains("static"))
                            {
                                StaticInitializers.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(vds.Identifier), vds.Initializer.Value)));
                            }
                            else
                            {
                                Initializers.Add(SyntaxFactory.ExpressionStatement(SyntaxFactory.BinaryExpression(SyntaxKind.SimpleAssignmentExpression, SyntaxFactory.IdentifierName(vds.Identifier), vds.Initializer.Value)));
                            }
                        }
                    }
                }
            }

            if (Initializers.Count == 0 && StaticInitializers.Count == 0)
                return node;
            SyntaxList<MemberDeclarationSyntax> newMembers = new SyntaxList<MemberDeclarationSyntax>();

            if (Initializers.Count > 0)
            {
                int constructors = node.Members.Count((m) => (m is ConstructorDeclarationSyntax && !((ConstructorDeclarationSyntax)m).Modifiers.ToString().Contains("static")));

                prector = prector.AddBodyStatements(Initializers.ToArray());
                node = node.AddMembers(prector);

                if (constructors == 0)
                {
                    ConstructorDeclarationSyntax ctor = SyntaxFactory.ConstructorDeclaration(node.Identifier);
                    ctor = ctor.AddBodyStatements(SyntaxFactory.ExpressionStatement(SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(".prector"))));
                    ctor = ctor.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

                    newMembers = newMembers.Add(ctor);
                }
               // else
                {

                    foreach (MemberDeclarationSyntax member in node.Members)
                    {
                        if (member.CSharpKind() == SyntaxKind.ConstructorDeclaration && !((ConstructorDeclarationSyntax)member).Modifiers.ToString().Contains("static"))
                        {
                            newMembers = newMembers.Add((MemberDeclarationSyntax)ConstructorPrefixerDeclaration((ConstructorDeclarationSyntax)member));
                        }
                        else
                        {
                            newMembers = newMembers.Add(member);
                        }
                    }
                }
            }
            if (StaticInitializers.Count > 0)
            {
                int constructors = node.Members.Count((m) => (m is ConstructorDeclarationSyntax && ((ConstructorDeclarationSyntax)m).Modifiers.ToString().Contains("static")));

                precctor = precctor.AddBodyStatements(StaticInitializers.ToArray());
                node = node.AddMembers(precctor);

                if (constructors == 0)
                {
                    ConstructorDeclarationSyntax ctor = SyntaxFactory.ConstructorDeclaration(node.Identifier);
                    ctor = ctor.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
                    ctor = ctor.AddBodyStatements(SyntaxFactory.ExpressionStatement(SyntaxFactory.InvocationExpression(SyntaxFactory.IdentifierName(".precctor"))));
                    ctor = ctor.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));

                    newMembers = newMembers.Add(ctor);
                }
                //else
                {

                    foreach (MemberDeclarationSyntax member in node.Members)
                    {
                        if (member.CSharpKind() == SyntaxKind.ConstructorDeclaration && ((ConstructorDeclarationSyntax)member).Modifiers.ToString().Contains("static"))
                        {
                            newMembers = newMembers.Add((MemberDeclarationSyntax)StaticConstructorPrefixerDeclaration((ConstructorDeclarationSyntax)member));
                        }
                        else
                        {
                            newMembers = newMembers.Add(member);
                        }
                    }
                }
            }

            return node.WithMembers(newMembers);
        }
开发者ID:LaxLacks,项目名称:ls2csc,代码行数:101,代码来源:FieldInitializerRewriter.cs


注:本文中的Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.WithMembers方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。