gpt4 book ai didi

c# - 被 SyntaxNode.ReplaceNodes<>() 搞糊涂了

转载 作者:行者123 更新时间:2023-11-30 14:35:31 29 4
gpt4 key购买 nike

我正在尝试用我自己构建的 InvocationExpressionSyntax 节点替换 InvocationExpressionSyntax 节点。

到目前为止,我发现的唯一方法是使用 SyntaxNode.ReplaceNodes<>,但我无法理解它的工作原理。

这是一个代码片段

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;
}));
}

方法的 Func<> arg 的元数据:

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.

没有其他方法可以替换我能找到的节点。即使我返回我构建的替换节点,它也只是返回原始节点。

如何简单地替换整个节点?

最佳答案

您的代码片段让我有些困惑。 ReplaceNodes 替换第一个参数中指定的节点,因此第一个参数应该是现有 节点的列表,而不是新节点。您可以在 lambda 中为第二个参数创建新节点,然后在树中替换这些节点。如果您必须知道当前正在替换哪个节点,请查看两个参数中的第一个(在您的示例中为“旧”)以了解它是什么。

关于c# - 被 SyntaxNode.ReplaceNodes<>() 搞糊涂了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11963106/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com