本文整理汇总了C#中SyntaxNode.ChildNodes方法的典型用法代码示例。如果您正苦于以下问题:C# SyntaxNode.ChildNodes方法的具体用法?C# SyntaxNode.ChildNodes怎么用?C# SyntaxNode.ChildNodes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SyntaxNode
的用法示例。
在下文中一共展示了SyntaxNode.ChildNodes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefaultVisit
public override void DefaultVisit(SyntaxNode node)
{
foreach (var child in node.ChildNodes())
{
child.Accept(this);
}
}
示例2: EnumerateNonRootChildren
private IEnumerable<SyntaxNode> EnumerateNonRootChildren(SyntaxNode node)
{
foreach (var child in node.ChildNodes())
{
if (LambdaUtilities.IsLambdaBodyStatementOrExpression(child))
{
continue;
}
if (HasLabel(child))
{
yield return child;
}
else
{
foreach (var descendant in child.DescendantNodes(DescendIntoChildren))
{
if (HasLabel(descendant))
{
yield return descendant;
}
}
}
}
}
示例3: DefaultVisit
public override void DefaultVisit(SyntaxNode node)
{
foreach (var child in node.ChildNodes())
{
Visit(child);
}
}
示例4: GetMethodDeclaration
private async Task<MethodDeclarationSyntax> GetMethodDeclaration(
SyntaxNode node,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
var invocationExpression = node.ChildNodes().FirstOrDefault(n => n.IsKind(SyntaxKind.InvocationExpression));
var methodSymbol = semanticModel.GetSymbolInfo(invocationExpression, cancellationToken).Symbol as IMethodSymbol;
if (methodSymbol == null)
{
return null;
}
var methodReference = methodSymbol.DeclaringSyntaxReferences.FirstOrDefault();
if (methodReference == null)
{
return null;
}
var methodDeclaration = (await methodReference.GetSyntaxAsync(cancellationToken).ConfigureAwait(false)) as MethodDeclarationSyntax;
if (methodDeclaration == null)
{
return null;
}
if (!methodDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.AsyncKeyword)))
{
return null;
}
return methodDeclaration;
}
示例5: Get
public override Token Get(SyntaxNode node)
{
var valueText = node
.DescendantNodes()
.OfType<IdentifierNameSyntax>()
.Last()
.Identifier.ValueText;
if (valueText == null)
{
throw new NotImplementedException();
}
var args = node.ChildNodes().OfType<ArgumentListSyntax>().First()
.ChildNodes().OfType<ArgumentSyntax>().ToArray();
var whenClause = args[0].ChildNodes().First();
var block = args[1]
.ChildNodes().OfType<ParenthesizedLambdaExpressionSyntax>().First()
.ChildNodes().OfType<BlockSyntax>().First();
var details = new WhenClosureDetails(whenClause, block);
return new Token("WhenClosure", details);
}
示例6: Visit
private void Visit(SyntaxNode node, int spaces)
{
Console.WriteLine(new string(' ', spaces) + textVisitor.Visit(node));
foreach (var child in node.ChildNodes())
{
Visit(child, spaces + 2);
}
}
示例7: Get
public override Token Get(SyntaxNode node)
{
var argument = node
.ChildNodes().OfType<ArgumentListSyntax>().First()
.ChildNodes().OfType<ArgumentSyntax>().First();
var tokenDetails = GetDetails(argument);
return new Token(Identifier(node).ToLower(), tokenDetails);
}
示例8: GetTargetNode
protected override SyntaxNode GetTargetNode(SyntaxNode node)
{
if (node.IsKind(SyntaxKind.MemberBindingExpression))
{
var nameNode = node.ChildNodes().FirstOrDefault(n => n.IsKind(SyntaxKind.IdentifierName));
if (nameNode != null)
{
return nameNode;
}
}
return base.GetTargetNode(node);
}
示例9: Visit
public override void Visit(SyntaxNode node)
{
int padding = node.Ancestors().Count();
//To identify leaf nodes vs nodes with children
string prepend = node.ChildNodes().Any() ? "[-]"
: "[.]";
//Get the type of the node
string line = new String(' ', padding) + prepend
+ " " + node.GetType().ToString();
//Write the line
System.Console.WriteLine(line);
base.Visit(node);
}
示例10: AddModifiers
private SyntaxNode AddModifiers(SyntaxNode node, SyntaxToken syntaxToken)
{
// Get the first child node.
SyntaxNode firstNode = node.ChildNodes().FirstOrDefault();
if (firstNode != null)
{
// The leading trivia will be the method comments, if any.
SyntaxTriviaList leadingTrivia = firstNode.GetLeadingTrivia();
// Replace it with one without leading trivia.
node = node.ReplaceNodes(
new[] { firstNode },
(a, b) => firstNode.WithLeadingTrivia());
// Add the leading trivia to the new modifier.
syntaxToken = syntaxToken.WithLeadingTrivia(leadingTrivia);
}
return node.AddModifiers(syntaxToken);
}
示例11: HasBlockChild
private bool HasBlockChild(SyntaxNode node)
{
return node.ChildNodes().Any(n => n.Kind == SyntaxKind.Block);
}
示例12: GetFirstBlockChild
private SyntaxNode GetFirstBlockChild(SyntaxNode node)
{
return node.ChildNodes().First(n => n.Kind == SyntaxKind.Block);
}
示例13: PrintPretty
/// <summary>
///algorithm copied from
/// http://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure.
/// </summary>
/// <param name="node"></param>
/// <param name="indent"></param>
/// <param name="last"></param>
/// <returns></returns>
private string PrintPretty(SyntaxNode node,string indent, bool last)
{
var sb = new StringBuilder();
sb.Append(indent);
if (last)
{
sb.Append("\\-");
indent += "\t";
}
else
{
sb.Append("|-");
indent += "|\t";
}
sb.AppendLine(node.Kind.ToString() + ":" + StringUtil.ReplaceNewLine(node.GetText(), ""));
for (int i = 0; i < node.ChildNodes().Count() ; i++)
{
var child = node.ChildNodes().ElementAt(i);
sb.Append(PrintPretty(child, indent, i == node.ChildNodes().Count() - 1));
}
return sb.ToString();
}
示例14: AddType
//.........这里部分代码省略.........
)
)
);
var equalsEqualsRef = Syntax.MemberAccessExpression(SyntaxKind.MemberAccessExpression, obj, Syntax.ParseToken("."), Syntax.IdentifierName("Equals"));
var equalsAccess = Syntax.InvocationExpression(equalsEqualsRef, Syntax.ArgumentList(Syntax.SeparatedList(Syntax.Argument(Syntax.ParseExpression("o")))));
var equalsIf =
Syntax.IfStatement(
Syntax.ParseExpression("other == null"),
Syntax.ReturnStatement(equalsAccess)
);
var equalsEqualsExps =
equivTo.GetMembers()
.OfType<PropertySymbol>()
.Select(
p =>
{
var n = p.Name;
ExpressionSyntax ret;
if (p.Type.IsReferenceType)
{
var strExp = "(" + n + " != null ? " + n + ".Equals(other." + n + ") : (other." + n + " != null ? other." + n + ".Equals(" + n + ") : true ))";
ret = Syntax.ParseExpression(strExp);
}
else
{
ret = Syntax.ParseExpression(n + " == other." + n);
}
return ret.WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")).WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "));
}
).ToList();
ExpressionSyntax equalsBinary = equalsEqualsExps.First();
for (var i = 1; i < equalsEqualsExps.Count; i++) equalsBinary = Syntax.BinaryExpression(SyntaxKind.LogicalAndExpression, equalsBinary, equalsEqualsExps[i]);
var equalsBinaryRet = Syntax.ReturnStatement(equalsBinary.WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")));
var equalsStatements =
Syntax.List(
(StatementSyntax)Syntax.LocalDeclarationStatement(equalsAssign),
(StatementSyntax)equalsIf,
(StatementSyntax)equalsBinaryRet
);
var equalsBody =
Syntax.Block(
Syntax.ParseToken("{"),
equalsStatements,
Syntax.ParseToken("}")
);
var equals =
Syntax.MethodDeclaration(
null,
Syntax.TokenList(Syntax.ParseToken("public").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")), Syntax.ParseToken("override").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" "))),
Syntax.ParseTypeName("bool").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
null,
Syntax.Identifier("Equals"),
null,
Syntax.ParameterList(
Syntax.SeparatedList(
Syntax.Parameter(
null,
Syntax.TokenList(),
Syntax.ParseTypeName("object").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
Syntax.Identifier("o"),
null
)
)
),
null,
equalsBody
);
members.Add(equals);
var equiv =
Syntax.ClassDeclaration(
null,
Syntax.ParseToken("internal").WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
Syntax.Identifier(withName).WithLeadingTrivia(Syntax.ParseLeadingTrivia(" ")).WithTrailingTrivia(Syntax.ParseTrailingTrivia(" ")),
null,
null,
null,
Syntax.List<MemberDeclarationSyntax>(members)
);
var namespaces = tree.ChildNodes().OfType<NamespaceDeclarationSyntax>();
if (namespaces.Count() == 0 || namespaces.Count() > 1)
{
// HACK, better way to insert classes should be considered
throw new Exception("Making some assumptions about namespaces, you can only have 1");
}
var @namespace = namespaces.Single();
var updated =
@namespace.WithMembers(
@namespace.Members.Add(equiv)
);
return tree.ReplaceNode(@namespace, updated);
}
示例15: LogSyntaxTree
private void LogSyntaxTree(SyntaxNode node, StringBuilder builder, int ident = 0, bool skipSelf = false)
{
string sident = String.Empty;
for (int i = 0; i < ident; ++i)
sident += "\t";
if (!skipSelf)
{
builder.AppendLine (sident + node.GetType() + ": " + node);
ident++;
}
foreach (SyntaxNode childNode in node.ChildNodes())
LogSyntaxTree (childNode, builder, ident);
}