I have a CSharpSyntaxRewriter that adds a new using directive to my file:-
public class AddUsingDirective : CSharpSyntaxRewriter
{
public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node)
{
// this adds the using directive inside the namespace not outside
node = node.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("MyCompany.MyProject.DataAccessLayer.Abstractions"))).NormalizeWhitespace();
return base.VisitNamespaceDeclaration(node);
}
}
The problem is that is adds the new using directive inside the namespace but I want to add it to the other existing namespaces above the namespace declaration. Any ideas how I can do this?
NamespaceDeclarationSyntaxit's too late -- the only thing you can change is the namespace declaration, and you want to change something outside of the namespace declaration. You need to be editing theCompilationUnit, see here