3

My question is about CSharpCompilerOptions and using statements passed to an instance of CSharpCompilation.

I have a syntax tree parsed from some text:

SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
            namespace RoslynCodeAnalysis
            {
                using System;
                using System.Net; // Commenting this out causes errors

                public class Writer
                {
                    public void Write(string message)
                    {
                        WebClient client = new WebClient();
                        Console.WriteLine(message); 
                    }
                }
            }");

The CSharpCompilation instance is created as below. I did not include the references for simplicity but it does add a reference to System.Net and few other common assemblies.

CSharpCompilation compilation = CSharpCompilation.Create(
            assemblyName,
            syntaxTrees: new[] { syntaxTree },
            references: references,
            options: new CSharpCompilationOptions(
                       OutputKind.DynamicallyLinkedLibrary, 
                       usings: new List<string> { "System", "System.Net" }));

The issue is that, if I comment out using System.Net, I get errors saying that:

CS0246: The type or namespace name 'WebClient' could not be found (are you missing a using directive or an assembly reference?)

What could cause this? The options passed to CSharpCompilation has the usings property set to System and System.Net. Is this the normal behavior or am I missing something? Even though the usings property is specified, source text still needs to have the using statement? I am pretty sure I am missing something!

4
  • I'm guessing here, but it appears that those are for the global namespace... note /// Global namespace usings. in source.roslyn.io/#Microsoft.CodeAnalysis.CSharp/… line 27. Those namespaces can be accessed under the global:: namespace alias msdn.microsoft.com/en-us/library/c3ay4x3d.aspx if I'm right, switch it to global::WebClient client = new global::WebClient(); and it will work with using System.Net; commented out. Commented Aug 5, 2016 at 18:17
  • Hello Will, thanks for suggestion, I tried it and still the same error. I did notice that comment in the source and I assumed that its "global" with respect to all the syntax trees, for the current unit of compilation. Commented Aug 5, 2016 at 18:46
  • @Will: No; that is not true. Commented Aug 5, 2016 at 19:10
  • 4
    @SLaks I said it was a guess, jeeeeeeeeez. Muh feelings. Commented Aug 5, 2016 at 19:36

1 Answer 1

5

This is used for scripting, not normal file compilations.

If you trace usages of that property through Roslyn source, you'll end up here, which is used in the interactive pipeline (that's what a Submission is).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.