2 years ago
#55618
Isolin
Set a parent for nodes produced by SyntaxFactory
I am using CSharpSyntaxRewriter
to traverse the syntax tree and SyntaxFactory
to replace some of the nodes, for the purpose of my own C#-like DSL.
When I create a new node, e.g. a MemberAccessExpression
, I would like to assign its Parent
to node.Parent
, but I have no idea how to do it. The Parent
property is only a getter, so I assume I need to place the node in the syntax tree somehow.
Calling ReplaceNode
on the parent of the input node has no effect. I think the returned node is irrelevant for my case, since I just need to achieve that newNode.Parent == node.Parent
. This is important in a few steps later inside of VisitMemberAccessExpression
. Any ideas or suggestions please?
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
{
if (node.Identifier.Text.StartsWith("@"))
{
var cleanName = node.Identifier.Text[1..];
var newNode = MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
IdentifierName("entity"),
IdentifierName(cleanName))
node.Parent.ReplaceNode(node, newNode); //no success, newNode.Parent still null
//before stepping in, I need newNode.Parent == node.Parent
return base.VisitMemberAccessExpression(newNode);
}
c#
roslyn
roslyn-code-analysis
c#
roslyn
roslyn-code-analysis
0 Answers
Your Answer