本文整理汇总了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);
}
示例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
}
示例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;
}
示例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));
}