2013-03-09 50 views
8

虽然与最新版本的工作,我发现它不支持dynamic关键字在编译和执行脚本,即你会得到一个编译错误error CS8000: This language feature ('dynamic') is not yet implemented in Roslyn.这里是一个简短的代码片段:Roslyn可以编译等待关键字吗?

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); 

工作时与await关键字...

与此相反,在编译或脚本await关键字工作时,我通常会得到一些随机的编译错误,像下列条件之一:

  • 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?)

脚本样本

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 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(); 

问题:我错过了什么,或者它还没有实现吗?

我尝试过不同的配置,其中LanguageVersion.CSharp5没有。谷歌和Stackoverflow搜索都充满了关于关键字的营销宣传,几乎没有用处。 Microsoft "Roslyn" CTP forum也没有答案。

PS:据我所知async关键字已经推出了可读性都被人类和编译器而await做所有魔法

回答

9

await支持在当前罗斯林CTP实现(虽然它是在现在实现内部构建)。

错误报告差异的原因在于我们首先构建了Roslyn分析器,以便它可以处理整个C#4语言,然后逐个填充特征的语义。由于await是C#5的特性,它甚至不被解析器识别,并且没有地方可以识别它的使用并提供良好的错误。

+6

任何有关此内部版本(或新版CTP)何时发布的计划? – JustAnotherUserYouMayKnow 2013-03-12 16:03:33

+0

真的很期待下一个代码下降!我希望能够生成利用System.Net.Http.HttpClient异步支持的类。 – 2013-05-08 13:37:30

5

其实,Roslyn论坛确实有答案。如果您查看帖子Known Limitations and Unimplemented Language Features,您会注意到它包含C#中尚未实现的功能中的“异步”。

这份名单是关于六月CTP,但由于the list of changes between the June CTP and the December CTP没有列出异步,这意味着它只是尚未实现。

我认为错误消息的差异的原因是,罗斯林确实懂得dynamic(但尚未实现它)。另一方面,它不明白async - await,所以它给你通用的编译错误。