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


C# SyntaxTree.GetCompilationUnitRoot方法代码示例

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


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

示例1: IsSelectingADifferentMethod

 private static bool IsSelectingADifferentMethod(IEnumerable<SyntaxNode> childNodes, SimpleNameSyntax methodName, SyntaxTree tree, IMethodSymbol methodSymbol, ExpressionSyntax invocationExpression, Compilation compilation)
 {
     var parameterExpressions = GetParameterExpressions(childNodes);
     var firstArgument = parameterExpressions.FirstOrDefault();
     var argumentList = CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1));
     var newInvocationStatement = CreateInvocationExpression(firstArgument, methodName, argumentList)
         .WithAdditionalAnnotations(introduceExtensionMethodAnnotation);
     var extensionMethodNamespaceUsingDirective = SyntaxFactory.UsingDirective(methodSymbol.ContainingNamespace.ToNameSyntax());
     var speculativeRootWithExtensionMethod = tree.GetCompilationUnitRoot()
         .ReplaceNode(invocationExpression, newInvocationStatement)
         .AddUsings(extensionMethodNamespaceUsingDirective);
     var speculativeModel = compilation.ReplaceSyntaxTree(tree, speculativeRootWithExtensionMethod.SyntaxTree)
         .GetSemanticModel(speculativeRootWithExtensionMethod.SyntaxTree);
     var speculativeInvocationStatement = speculativeRootWithExtensionMethod.SyntaxTree.GetCompilationUnitRoot().GetAnnotatedNodes(introduceExtensionMethodAnnotation).Single() as InvocationExpressionSyntax;
     var speculativeExtensionMethodSymbol = speculativeModel.GetSymbolInfo(speculativeInvocationStatement.Expression).Symbol as IMethodSymbol;
     var speculativeNonExtensionFormOfTheMethodSymbol = speculativeExtensionMethodSymbol?.GetConstructedReducedFrom();
     return speculativeNonExtensionFormOfTheMethodSymbol == null || !speculativeNonExtensionFormOfTheMethodSymbol.Equals(methodSymbol);
 }
开发者ID:haroldhues,项目名称:code-cracker,代码行数:18,代码来源:CallExtensionMethodAsExtensionAnalyzer.cs

示例2: IsSelectingADifferentMethod

 private static bool IsSelectingADifferentMethod(IEnumerable<SyntaxNode> childNodes, SimpleNameSyntax methodName, SyntaxTree tree, IMethodSymbol methodSymbol, ExpressionSyntax invocationExpression, Compilation compilation)
 {
     var parameterExpressions = GetParameterExpressions(childNodes);
     var firstArgument = parameterExpressions.FirstOrDefault();
     var argumentList = CreateArgumentListSyntaxFrom(parameterExpressions.Skip(1));
     var newInvocationStatement = CreateInvocationExpression(firstArgument, methodName, argumentList)
         .WithAdditionalAnnotations(introduceExtensionMethodAnnotation);
     var extensionMethodNamespaceUsingDirective = SyntaxFactory.UsingDirective(methodSymbol.ContainingNamespace.ToNameSyntax());
     var speculativeRootWithExtensionMethod = tree.GetCompilationUnitRoot()
         .ReplaceNode(invocationExpression, newInvocationStatement)
         .AddUsings(extensionMethodNamespaceUsingDirective);
     var speculativeTree = speculativeRootWithExtensionMethod.SyntaxTree;
     var speculativeTreeOptions = (CSharpParseOptions)speculativeTree.Options;
     var speculativeTreeWithCorrectLanguageVersion = speculativeTree.WithRootAndOptions(speculativeRootWithExtensionMethod, speculativeTreeOptions.WithLanguageVersion(((CSharpParseOptions)tree.Options).LanguageVersion));
     var speculativeModel = compilation.ReplaceSyntaxTree(tree, speculativeTreeWithCorrectLanguageVersion)
         .GetSemanticModel(speculativeTreeWithCorrectLanguageVersion);
     var speculativeInvocationStatement = speculativeTreeWithCorrectLanguageVersion.GetCompilationUnitRoot().GetAnnotatedNodes(introduceExtensionMethodAnnotation).Single() as InvocationExpressionSyntax;
     var speculativeExtensionMethodSymbol = speculativeModel.GetSymbolInfo(speculativeInvocationStatement.Expression).Symbol as IMethodSymbol;
     var speculativeNonExtensionFormOfTheMethodSymbol = speculativeExtensionMethodSymbol?.GetConstructedReducedFrom();
     return speculativeNonExtensionFormOfTheMethodSymbol == null || speculativeNonExtensionFormOfTheMethodSymbol.ToString() != methodSymbol.ToString();//can't compare equality, as speculative symbol might be different
 }
开发者ID:julianosaless,项目名称:code-cracker,代码行数:21,代码来源:CallExtensionMethodAsExtensionAnalyzer.cs

示例3: Genesis

        public static SyntaxTree Genesis(SyntaxTree syntaxTree)
        {
            CodeGenSyntaxVisitor cbs = new CodeGenSyntaxVisitor(syntaxTree.GetCompilationUnitRoot());
            cbs.Visit();

            var @class = SyntaxFactory.ClassDeclaration("MyGenerator")
                .AddModifiers(SyntaxFactory.Token(SyntaxKind.PartialKeyword))
                .AddMembers(cbs.Generated.ToArray());

            var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.IdentifierName("MyGenerator"))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Collections.Generic")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Linq")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Text")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("System.Reflection")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis.CSharp")))
                .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.IdentifierName("Microsoft.CodeAnalysis.CSharp.Syntax")))
                .AddMembers(@class);

            syntaxTree = SyntaxFactory.SyntaxTree(@namespace, path: "output");

            return syntaxTree;
        }
开发者ID:dpetillo,项目名称:genesis,代码行数:24,代码来源:GenesisDevice.cs

示例4: CheckOverloadResolutionResults

 private static void CheckOverloadResolutionResults(SyntaxTree tree, SemanticModel model, params string[] expected)
 {
     var actual = GetElementAccessExpressions(tree.GetCompilationUnitRoot()).Select(syntax => model.GetSymbolInfo(syntax).Symbol.ToTestDisplayString());
     AssertEx.Equal(expected, actual, itemInspector: s => string.Format("\"{0}\"", s));
 }
开发者ID:Rickinio,项目名称:roslyn,代码行数:5,代码来源:IndexerTests.cs


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