/* method is instance of MethodDeclarationSyntax */
BlockSyntax newBody = SyntaxFactory.Block(SyntaxFactory.ParseStatement("throw new NotImplementedException();"));
BlockSyntax body = method.Body;
var modifiedMethod = method.ReplaceNode(body, newBody);
但是当我这样做时,方法后面的换行符会被删除,如果在方法后面有一个 #region 或者 #endregion 标签,将会出现错误。
例如:
#region
static void RemoveRegions(string str)
{
return;
}
#endregion
替换body后
#region
static void RemoveRegions(string str)
{
throw new NotImplementedException();
} #endregion // This cause to compiling error
File.WriteAllText(newDoc.FilePath,code);
来保存我的新文档(Document 的实例)。也许问题就出在这里。 - Ali Sepehri.Kh