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;";
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 expectederror CS1003: Syntax error, ',' expectederror 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