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


C# CompilationUnitSyntax.ReplaceNodes方法代码示例

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


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

示例1: ImplementINotifyPropertyChanged

        internal static CompilationUnitSyntax ImplementINotifyPropertyChanged(CompilationUnitSyntax root, SemanticModel model, IEnumerable<ExpandablePropertyInfo> properties, Workspace workspace)
        {
            var typeDeclaration = properties.First().PropertyDeclaration.FirstAncestorOrSelf<TypeDeclarationSyntax>();
            var backingFieldLookup = Enumerable.ToDictionary(properties, info => info.PropertyDeclaration, info => info.BackingFieldName);

            root = root.ReplaceNodes(properties.Select(p => p.PropertyDeclaration as SyntaxNode).Concat(new[] { typeDeclaration }),
                (original, updated) => original.IsKind(SyntaxKind.PropertyDeclaration)
                    ? ExpandProperty((PropertyDeclarationSyntax)original, backingFieldLookup[(PropertyDeclarationSyntax)original]) as SyntaxNode
                    : ExpandType((TypeDeclarationSyntax)original, (TypeDeclarationSyntax)updated, properties.Where(p => p.NeedsBackingField), model, workspace));

            return root
                .WithUsing("System.Collections.Generic")
                .WithUsing("System.ComponentModel");
        }
开发者ID:CAPCHIK,项目名称:roslyn,代码行数:14,代码来源:CodeGeneration.cs

示例2: ImplementFullProperty

        internal static CompilationUnitSyntax ImplementFullProperty(CompilationUnitSyntax root, SemanticModel model, PropertyDeclarationSyntax propertyDecl, Workspace workspace)
        {
            TypeDeclarationSyntax typeDecl = propertyDecl.FirstAncestorOrSelf<TypeDeclarationSyntax>();
            string propertyName = propertyDecl.Identifier.ValueText;
            string backingFieldName = $"_{char.ToLower(propertyName[0])}{propertyName.Substring(1)}";
            ITypeSymbol propertyTypeSymbol = model.GetDeclaredSymbol(propertyDecl).Type;

            root = root.ReplaceNodes(new SyntaxNode[] { propertyDecl, typeDecl },
                (original, updated) =>
                original.IsKind(SyntaxKind.PropertyDeclaration)
                ? ExpandProperty((PropertyDeclarationSyntax)original, (PropertyDeclarationSyntax)updated, backingFieldName) as SyntaxNode
                : ExpandType((TypeDeclarationSyntax)original, (TypeDeclarationSyntax)updated, propertyTypeSymbol, backingFieldName, model, workspace) as SyntaxNode
            );

            return root;
        }
开发者ID:CNinnovation,项目名称:TechConference2016,代码行数:16,代码来源:CodeGeneration.cs

示例3: TransformRoot

            public CompilationUnitSyntax TransformRoot(CompilationUnitSyntax root)
            {
                root = root.ReplaceNodes(_nodeToAnnotations.Keys, (originalNode, rewrittenNode) =>
                {
                    var ret = rewrittenNode.WithAdditionalAnnotations(_nodeToAnnotations[originalNode]);

                    return ret;
                });

                foreach (var kvp in _annotationToTransformation)
                {
                    Dictionary<SyntaxNode, SyntaxNode> originalNodeMap = new Dictionary<SyntaxNode, SyntaxNode>();
                    foreach (var originalNodeKvp in _originalNodeLookup)
                    {
                        var annotatedNodes = root.GetAnnotatedNodes(originalNodeKvp.Key).ToList();
                        SyntaxNode annotatedNode = annotatedNodes.SingleOrDefault();
                        if (annotatedNode != null)
                        {
                            originalNodeMap[annotatedNode] = originalNodeKvp.Value;
                        }
                    }

                    var syntaxAnnotation = kvp.Key;
                    var transformation = kvp.Value;
                    var nodesToTransform = root.GetAnnotatedNodes(syntaxAnnotation);
                    root = transformation(root, nodesToTransform, originalNodeMap);
                }

                return root;
            }
开发者ID:chuck-mitchell,项目名称:codeformatter,代码行数:30,代码来源:MSTestToXUnitConverter.cs

示例4: DumpEvents

        static CompilationUnitSyntax DumpEvents(CompilationUnitSyntax root)
        {
            Dictionary<SyntaxNode, SyntaxNode> changes = new Dictionary<SyntaxNode, SyntaxNode>();

            var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
            foreach (var @class in classes)
            {
                var events = @class.Members.OfType<EventDeclarationSyntax>();
                foreach (var @event in events)
                {
                    AccessorDeclarationSyntax add, remove;
                    BlockSyntax newBody = SyntaxFactory.Block(SyntaxFactory.ParseStatement("throw new NotImplementedException();"));
                    AccessorDeclarationSyntax adder = SyntaxFactory.AccessorDeclaration(SyntaxKind.AddAccessorDeclaration,newBody);
                    AccessorDeclarationSyntax remover = SyntaxFactory.AccessorDeclaration(SyntaxKind.RemoveAccessorDeclaration, newBody);
                    SyntaxList<AccessorDeclarationSyntax> syntaxes = new SyntaxList<AccessorDeclarationSyntax>();
                    syntaxes = syntaxes.Add(adder);
                    syntaxes = syntaxes.Add(remover);
                    AccessorListSyntax accessors = SyntaxFactory.AccessorList(syntaxes);
                    
                    EventDeclarationSyntax modifiedEvent = @event.WithAccessorList(accessors);
                    changes.Add(@event, modifiedEvent);
                }

            }

            root = root.ReplaceNodes(changes.Keys, (n1, n2) => changes[n1]);
            return root;
        }
开发者ID:AliSepehri,项目名称:RoslynCodeModifier,代码行数:28,代码来源:Program.cs

示例5: DumpProperties

        static CompilationUnitSyntax DumpProperties(CompilationUnitSyntax root)
        {
            Dictionary<SyntaxNode, SyntaxNode> changes = new Dictionary<SyntaxNode, SyntaxNode>();

            var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
            foreach (var @class in classes)
            {
                var properties = @class.Members.OfType<PropertyDeclarationSyntax>();
                foreach (var property in properties)
                {
                    AccessorDeclarationSyntax getter, setter;
                    TryGetAccessors(property, out getter, out setter);

                    BlockSyntax newBody = SyntaxFactory.Block(SyntaxFactory.ParseStatement("throw new NotImplementedException();"));
                    BlockSyntax getterBody = null;
                    BlockSyntax setterBody = null;
                    PropertyDeclarationSyntax modifiedProperty = null;
                    if (getter != null)
                    {
                        getterBody = getter.Body;
                        if (getterBody == null) continue;
                        changes.Add(getterBody, newBody);
                    }

                    if (setter != null)
                    {
                        setterBody = setter.Body;
                        if (setterBody == null) continue;
                        changes.Add(setterBody, newBody);
                    }
                }
                
            }

            root = root.ReplaceNodes(changes.Keys, (n1, n2) => changes[n1]);
            return root;
        }
开发者ID:AliSepehri,项目名称:RoslynCodeModifier,代码行数:37,代码来源:Program.cs

示例6: DumpConstructors

        static CompilationUnitSyntax DumpConstructors(CompilationUnitSyntax root)
        {
            Dictionary<SyntaxNode, SyntaxNode> changes = new Dictionary<SyntaxNode, SyntaxNode>();

            var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();
            foreach (var @class in classes)
            {
                var constructors = @class.Members.OfType<ConstructorDeclarationSyntax>();
                var destructors = @class.Members.OfType<DestructorDeclarationSyntax>();
                
                foreach (var c in constructors)
                {
                    BlockSyntax newBody = SyntaxFactory.Block(SyntaxFactory.ParseStatement("throw new NotImplementedException();"));
                    BlockSyntax body = c.Body;
                    if (body == null) continue;
                    var modifiedMethod = c.ReplaceNode(body, newBody);
                    changes.Add(c, modifiedMethod);
                }

                foreach (var c in destructors)
                {
                    BlockSyntax newBody = SyntaxFactory.Block(SyntaxFactory.ParseStatement("throw new NotImplementedException();"));
                    BlockSyntax body = c.Body;
                    if (body == null) continue;
                    var modifiedMethod = c.ReplaceNode(body, newBody);
                    changes.Add(c, modifiedMethod);
                }
            }

            root = root.ReplaceNodes(changes.Keys, (n1, n2) => changes[n1]);
            return root;
        }
开发者ID:AliSepehri,项目名称:RoslynCodeModifier,代码行数:32,代码来源:Program.cs


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