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


C# MemberDeclarationSyntax.Ancestors方法代码示例

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


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

示例1: ExtractToExpressionTree

        private async Task<Document> ExtractToExpressionTree(Document document, MemberDeclarationSyntax declaration, CancellationToken c)
        {
            var typeSyntax = declaration.Ancestors().OfType<TypeDeclarationSyntax>().First();

            var sm = await document.GetSemanticModelAsync(c);

            var type = sm.GetDeclaredSymbol(typeSyntax, c);

            var symbol = sm.GetDeclaredSymbol(declaration);

            string name = declaration is MethodDeclarationSyntax ?
                ((MethodDeclarationSyntax)declaration).Identifier.ToString() :
                ((PropertyDeclarationSyntax)declaration).Identifier.ToString();

            string candidateName = name + "Expression";

            if (type.MemberNames.Contains(candidateName))
                candidateName = Enumerable.Range(2, 1000).Select(i => name + "Expression" + i).First(n => !type.MemberNames.Contains(n));

            List<ParameterSyntax> parameterList = GetParameters(declaration, type, symbol);

            TypeSyntax returnType = GetReturnType(declaration);

            var bodyExpression = GetSingleBody(declaration);
            var newField = GetStaticField(candidateName, parameterList, returnType, bodyExpression, sm, type);

            var newBody = GetNewBody(candidateName, parameterList);

            var newDeclaration = declaration.ReplaceNode(bodyExpression, newBody);

            MoveInitialTrivia(ref newField, ref newDeclaration);

            var newMembers = typeSyntax.Members.ReplaceRange(declaration, new[] { newField, newDeclaration });
            var newType = typeSyntax is StructDeclarationSyntax ?
                (TypeDeclarationSyntax)((StructDeclarationSyntax)typeSyntax).WithMembers(newMembers) :
                (TypeDeclarationSyntax)((ClassDeclarationSyntax)typeSyntax).WithMembers(newMembers);

            var docRoot = await document.GetSyntaxRootAsync();

            var newDoc = docRoot.ReplaceNode(typeSyntax, newType);

            var usings = newDoc.ChildNodes().OfType<UsingDirectiveSyntax>();

            if (usings.Any() && !usings.Any(a => a.Name.ToString() == "System.Linq.Expressions"))
                newDoc = newDoc.InsertNodesAfter(usings.LastOrDefault(), new[] {
                    SyntaxFactory.UsingDirective(
                        SyntaxFactory.IdentifierName("System").Qualified("Linq").Qualified("Expressions")
                        ).NormalizeWhitespace().WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed)
                });

            return document.WithSyntaxRoot(newDoc);
        }
开发者ID:rondoo,项目名称:framework,代码行数:52,代码来源:ExpressionFieldFixProvider.cs


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