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


C# MethodDeclarationSyntax.Ancestors方法代码示例

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


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

示例1: MissingAnalysisMethodAsync

        // adds an analysis method to the enclosing class
        private async Task<Document> MissingAnalysisMethodAsync(Document document, MethodDeclarationSyntax declaration, CancellationToken c)
        {
            SyntaxGenerator generator = SyntaxGenerator.GetGenerator(document);

            string analysisMethodName = CodeFixHelper.AnalysisMethodName(declaration);

            SemanticModel semanticModel = await document.GetSemanticModelAsync();
            var newAnalysisMethod = CodeFixHelper.CreateAnalysisMethod(generator, analysisMethodName, semanticModel) as MethodDeclarationSyntax;

            ClassDeclarationSyntax classDeclaration = declaration.Ancestors().OfType<ClassDeclarationSyntax>().First();

            return await ReplaceNode(classDeclaration, classDeclaration.AddMembers(newAnalysisMethod), document);
        }
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:14,代码来源:CodeFixProvider.cs

示例2: MethodFullName

 /// <summary>
 /// returns fullname of a methoddeclaration
 /// </summary>
 /// <param name="m"></param>
 /// <returns></returns>
 private string MethodFullName(MethodDeclarationSyntax m)
 {
     //may be a simplest way exists, but it works
     string result = m.Identifier.Text;
     var ancestors = m.Ancestors();
     SyntaxNode classOrStruct;
     var classe = ancestors.OfType<ClassDeclarationSyntax>().FirstOrDefault();
     if (classe != null)
     {
         result = classe.Identifier.Text + "." + result;
         classOrStruct = classe;
     }
     else
     {
         var structe = ancestors.OfType<StructDeclarationSyntax>().FirstOrDefault();
         if (structe == null)
         {
             //it's just a peace of code with out class or struct
             return result;
         }
         result = structe.Identifier.Text + "." + result;
         classOrStruct = structe;
     }
     var ns = classOrStruct.Ancestors().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
     var nsString = ns?.Name.ToString();
     result = nsString + "." + result;
     return result;
 }
开发者ID:pgourlain,项目名称:CodeWeaver,代码行数:33,代码来源:DocumentWeaver.cs

示例3: CreateMethodSyncVariant

        /// <summary>
        /// Creates a sync method variant for the given async method.
        /// </summary>
        /// <param name="document">The code analysis document.</param>
        /// <param name="methodDecl">The async method.</param>
        /// <param name="semanticModel">A semantic model to look for the type in.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>The modified document.</returns>
        private async Task<Document> CreateMethodSyncVariant(Document document, MethodDeclarationSyntax methodDecl, SemanticModel semanticModel, CancellationToken cancellationToken)
        {
            //  Setup
            MethodDeclarationSyntax syncMethodDecl;

            //  Are we dealing with interface or class?
            var typeDecl = methodDecl.Ancestors().OfType<TypeDeclarationSyntax>().FirstOrDefault();

            if (typeDecl is ClassDeclarationSyntax)
            {
                //  Create the async method signature
                syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariant(document, methodDecl, semanticModel, cancellationToken);
            }
            else
            {
                //  Create the async method signature
                syncMethodDecl = await CodeGeneration.ImplementSyncMethodVariantSignature(document, methodDecl, semanticModel, cancellationToken);
            }


            //  Update the solution
            var root = await document.GetSyntaxRootAsync(cancellationToken);

            //  Insert
            document = document.WithSyntaxRoot(root.InsertNodesBefore(methodDecl, new[] { syncMethodDecl }));

            //  Format
            document = await Formatter.FormatAsync(document, Formatter.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);
            document = await Simplifier.ReduceAsync(document, Simplifier.Annotation, cancellationToken: cancellationToken).ConfigureAwait(false);

            return document;
        }
开发者ID:QuartzTechnologies,项目名称:Asyncable,代码行数:40,代码来源:CodeRefactoringProvider.cs


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