本文整理汇总了C#中SyntaxNode.AncestorsAndSelf方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.AncestorsAndSelf方法的具体用法?C# SyntaxNode.AncestorsAndSelf怎么用?C# SyntaxNode.AncestorsAndSelf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.AncestorsAndSelf方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetOutsideTypeQualifiedName
private string GetOutsideTypeQualifiedName(SyntaxNode node)
{
// Get the name space name enclosing this node.
string namespaceName = node.AncestorsAndSelf().
// ancestors whose kind is name space node.
Where(n => n.Kind == SyntaxKind.NamespaceDeclaration).
// conver to the syntax and get the name.
Select(n => (NamespaceDeclarationSyntax)n).First().Name.PlainName;
// Get the class name enclosing this node.
var classesNames = node.AncestorsAndSelf().
// ancestors whose kind is class node.
Where(n => n.Kind == SyntaxKind.ClassDeclaration).
// convert each one to the kind class node syntax.
Select(n => (ClassDeclarationSyntax)n).
// order all the class decs by their length, in decending order.
OrderByDescending(n => n.Span.Length).
// select their names.
Select(n => n.Identifier.ValueText);
// Combine all the names to get the scope string.
var qualifiedName = namespaceName + "." + StringUtil.ConcatenateAll(".", classesNames);
logger.Info(qualifiedName);
return qualifiedName;
}
示例2: TryGetStatementEnclosingInvocation
/// <summary>
/// Try to get the statement syntax node that is enclosing the given method invocation.
/// </summary>
/// <param name="invocation"></param>
/// <param name="statement"></param>
/// <returns></returns>
private bool TryGetStatementEnclosingInvocation(SyntaxNode invocation, out SyntaxNode statement)
{
var statements = invocation.AncestorsAndSelf().OfType<StatementSyntax>().ToList();
if(statements.Any())
{
statement = statements.First();
return true;
}
statement = null;
return false;
}