I’m trying to replace an InvocationExpressionSyntax node with my own constructed InvocationExpressionSyntax node.
The only way I’ve found so far is using SyntaxNode.ReplaceNodes<>, but I’m having trouble wrapping my head around how it works.
Here’s a code snip
public override SyntaxNode VisitInvocationExpression(InvocationExpressionSyntax node)
{
InvocationExpressionSyntax replacementNode = (constructing my own invocation node, etc.
node = (InvocationExpressionSyntax)node.ReplaceNodes(new List<InvocationExpressionSyntax>() { replacementNode /* ##Solution: This should be the EXISTING node, not the replacement node*/ }, new Func<InvocationExpressionSyntax, InvocationExpressionSyntax, SyntaxNode>((old, old2) =>
{
//return what? old and old2 are both unmodified at this point. Doing another node.replacenodes in here doesn't work either. How do I replace node?
//even the following doesn't work.
return replacementNode;
}));
}
The metadata for the Func<> arg of the method:
A function that computes a replacement node for the argument nodes. The first
argument is the original node. The second argument is the same node rewritten
with replaced descendants.
There’s no other way to replace a node that I can find. Even when I return the replacement node I built, it just returns the original node.
How do I simply replace an entire node?
Your code snippet confuses me a little bit. ReplaceNodes replaces the nodes specified in the first argument, so the first argument should be a list of existing nodes, not new nodes. You can just create your new nodes in the lambda for the second argument, and those are substituted in the tree. If you have to know which of the nodes you’re currently replacing, look at the first of the two arguments (“old” in your example) to know what it is.