Andry Andry - 1 year ago 75
C# Question

Incorrect syntax is generated when creating a UsingDirective from factory in Roslyn

I am using Roslyn in order to generate a tree from another one. So we are dealing with AST transformation. I am using

in order to generate nodes.

In the specific case I want to create a u
directive that should look like this:

using MyNamespace.SubNamespace;

So I do:

var usingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("MyNamespace.SubNamespace"));
var newNode = mynode.AddUsings(new[] { usingDirective });

But if I inspect the final tree
which is generated (the string source code generated by simply calling
), I see that my directive has been added like this:


I can see the same thing if I just do:
. It seems very wrong, a space is needed and that should trigger a syntax error. What is going on?

Answer Source

Add this to fix the problem:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download