Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将节点添加到语法树会添加一个子节点,但实际上它不会显示_C#_Abstract Syntax Tree_Nrefactory - Fatal编程技术网

C# 将节点添加到语法树会添加一个子节点,但实际上它不会显示

C# 将节点添加到语法树会添加一个子节点,但实际上它不会显示,c#,abstract-syntax-tree,nrefactory,C#,Abstract Syntax Tree,Nrefactory,我正在编写一个VSPackage,它需要根据测试更改一个类以返回正确的值。简单地更改返回值很容易,但现在我尝试添加if-else语句,以便该类根据输入返回不同的值: var node = new IfElseStatement { Condition = new BinaryOperatorExpression { Left = new IdentifierExpression("parameterName"), Operator = Binary

我正在编写一个VSPackage,它需要根据测试更改一个类以返回正确的值。简单地更改返回值很容易,但现在我尝试添加if-else语句,以便该类根据输入返回不同的值:

var node = new IfElseStatement
{
    Condition = new BinaryOperatorExpression
    {
        Left = new IdentifierExpression("parameterName"),
        Operator = BinaryOperatorType.Equality,
        Right = new PrimitiveExpression(3)
    },
    TrueStatement = new ReturnStatement
    {
        Expression = new PrimitiveExpression(9)
    }
};

//Visitor finds the beginnning of the method (first child is '{' so insert after that)
var parentNodeToInsert = _abstractSyntaxTree.AcceptVisitor(new FindBeginningOfMethodVisitor());
parentNodeToInsert.InsertChildAfter(parentNodeToInsert.FirstChild, node, new Role<IfElseStatement>("Statement"));
如果在此之后放置断点并检查parentNodeToInsert:

在parentNodeToInsert蓝色框的子项下,它已正确添加,但是节点红色框的摘要不包含新的if语句,稍后如果我调用语法树上的toString将其写回文件,它也不会出现

有人知道为什么,以及如何显示吗?

是InsertChildAfter没有处理不可变的数据,因此您必须使用返回值?@leppie我不确定parentNodeToInsert为什么是不可变的。“使用返回值”是指parentNodeToInsert=parentNodeToInsert.InsertChildAfter。。。;?