4

I am embedding a C# script engine into my application using Roslyn and so far, I can execute code with no issues. I can for example execute the following code:

using System;
var str = "Hello Roslyn";
Console.WriteLine(str);

I am facing compilation issues when building my syntax tree out the code snippet above. The compiler complains about statements directly embedded into the main namespace which makes sense when writing a normal C# program but not in my case as I am going the script way.

Question: Is there a way to build an error free syntax tree out of a C# script?

EDIT Here is the code I am using to build the syntax tree.

SyntaxTree tree = SyntaxTree.ParseText(context.SourceCode);
Compilation compilation = Compilation.Create("CSharp", syntaxTrees: new[] {tree}, references: references);

Thank you

1
  • Can you update your question to include the code you are currently using to build the syntax tree? Commented Nov 28, 2012 at 15:48

1 Answer 1

8

You need to specify that you're parsing script code instead of regular code:

SyntaxTree tree = SyntaxTree.ParseText(source, options: new ParseOptions(kind: SourceCodeKind.Script));
Sign up to request clarification or add additional context in comments.

1 Comment

Man I wish I could give you a +100! I have been looking for this for weeks!!! Thank you very much.

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.