虽然与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