[英]How to add a using directive outside a namespace using Roslyn?
I have a CSharpSyntaxRewriter that adds a new using directive to my file:-我有一个 CSharpSyntaxRewriter,它向我的文件添加了一个新的 using 指令:-
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.问题是在命名空间内添加了新的 using 指令,但我想将它添加到命名空间声明上方的其他现有命名空间。 Any ideas how I can do this?
我有什么想法可以做到这一点?
public override SyntaxNode VisitCompilationUnit(CompilationUnitSyntax 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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.