Can Roslyn compile a wait keyword?

When working with the latest version of roslyn-ctp, I found that it does not support the dynamic keyword during compilation and script execution, that is, you will receive the error compilation error CS8000: This language feature ('dynamic') is not yet implemented in Roslyn. Here is the code snippet:

 var engine = new ScriptEngine(); var script = @"dynamic someVariable = 0;"; // you an error CS8000: This language feature ('dynamic') is not yet implemented in Roslyn engine.CreateSession().Execute(script); 

When working with the await keyword

In contrast, when working with the await keyword when compiling or a script, I usually got some compilation error, like one of the following:

  • error CS1001: Identifier expected
  • error CS1003: Syntax error, ',' expected
  • error CS0246: The type or namespace name 'await' could not be found (are you missing a using directive or an assembly reference?)

Sample scenarios

 var engine = new ScriptEngine(); new[] { "System", "System.Threading", "System.Threading.Tasks", } .ToList().ForEach(@namespace => engine.ImportNamespace(@namespace)); var script = @"await Task.Run(() => System.Console.WriteLine(""Universal [async] answer is '42'""));"; engine.CreateSession().Execute(script); 

Compilation example

 // compilation sample const string codeSnippet = @"namespace DemoNamespace { using System; using System.Threading; using System.Threading.Tasks; public class Printer { public async void Answer() { var answer = 42; var task = Task.Run(() => System.Console.WriteLine(string.Format(""Universal [async] answer is '{0}'"", answer))); await task; // not working task.Wait(); // working as expected } } }"; var syntaxTree = SyntaxTree.ParseText(codeSnippet, options: new ParseOptions(languageVersion: LanguageVersion.CSharp5)); var references = new [] { MetadataReference.CreateAssemblyReference(typeof(Console).Assembly.FullName), MetadataReference.CreateAssemblyReference(typeof(System.Threading.Tasks.Task).Assembly.FullName), }; var compilation = Compilation.Create( outputName: "Demo", options: new CompilationOptions(OutputKind.DynamicallyLinkedLibrary), syntaxTrees: new[] { syntaxTree }, references: references); if(compilation.GetDiagnostics().Any()) { compilation.GetDiagnostics().Select(diagnostic => diagnostic).Dump(); throw new Exception("Compilation failed"); } Assembly compiledAssembly; using (var stream = new MemoryStream()) { EmitResult compileResult = compilation.Emit(stream); compiledAssembly = Assembly.Load(stream.GetBuffer()); } dynamic instance = Activator.CreateInstance(compiledAssembly.GetTypes().First()); instance.Answer(); 

Q : Am I missing something or not yet implemented?

I tried a different configuration with and without LanguageVersion.CSharp5 . Google searches and Stackoverflow are full of marketing ads for roslyn and async keywords and almost useless. The CTP forum for Microsoft "Roslyn" also has no answer for this.

ps: as far as I know, the async introduced for readability by both people and compilers, while await does all the magic

+8
c # async-await roslyn
source share
2 answers

await support is not implemented in the current Roslyn CTP (although it is now implemented in internal assemblies).

The reason for the difference in the error messages is that we first created the Roslyn parser so that it can process the entire C # 4 language, and then populate the semantics for the functions one at a time. Since await is a C # 5 function, it is not even recognized by the parser, and there is no way to recognize its use and provide a good error.

+9
source share

Actually, the Roslyn forum has an answer. If you look at the Known Limitations and Possibilities of Unfulfilled Languages message, you will notice that it contains "Async" among functions not yet implemented in C #.

This list refers to the June CTP, but since the change list between the June CTP and the December CTP does not contain async, it means that it just has not been implemented yet.

I think the reason for the difference in the error message is because Roslyn understands dynamic (but does not yet implement it). On the other hand, it does not understand async - await , so it gives you general compilation errors.

+5
source share

All Articles