public ClassDeclarationSyntax Execute(
ClassDeclarationSyntax childDeclaration,
SemanticModel semantic,
Settings settings = null)
{
// we need the class name of the child to find it in the syntax tree
// after it was changed
var childClassName = childDeclaration.Identifier.Text.ToString();
foreach (var command in _commands)
{
var oldSyntaxTree = childDeclaration.SyntaxTree;
var newChildDeclaration = command.Execute(childDeclaration, semantic, settings);
// replace the old class declaration with the new one in the syntax tree
var newRoot = semantic.SyntaxTree.GetRoot().ReplaceNode(childDeclaration, newChildDeclaration);
var newSyntaxTree = newRoot.SyntaxTree;
var compilation = semantic.Compilation.ReplaceSyntaxTree(oldSyntaxTree,newSyntaxTree);
// after creating the new syntax tree, get a new semantic model
semantic = compilation.GetSemanticModel(newSyntaxTree);
// also retrieve the class declaration from the new syntax tree
childDeclaration = newRoot.FindClassByName(childClassName);
}
return childDeclaration;
}
}