2013-03-09 52 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,所以它給你通用的編譯錯誤。