雖然與roslyn-ctp最新版本的工作,我發現它不支持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搜索都充滿了關於roslyn和async關鍵字的營銷宣傳,幾乎沒有用處。 Microsoft "Roslyn" CTP forum也沒有答案。
PS:據我所知async
關鍵字已經推出了可讀性都被人類和編譯器而await
做所有魔法
任何有關此內部版本(或新版CTP)何時發佈的計劃? – JustAnotherUserYouMayKnow 2013-03-12 16:03:33
真的很期待下一個代碼下降!我希望能夠生成利用System.Net.Http.HttpClient異步支持的類。 – 2013-05-08 13:37:30