我如何执行脚本与罗斯林在最终用户预览
本文关键字:罗斯林 用户 脚本 何执行 执行 | 更新日期: 2023-09-27 18:03:06
我正在尝试玩roslyn的最终用户预览,并想执行一个简单的脚本。我想做的是:
static void Main(string[] args)
{
// Is this even valid?
var myScript = "int x = 5; int y = 6; x + y;";
// What should I do here?
var compiledScript = Something.Compile(myScript);
var result = compiledScript.Execute(myScript);
Console.WriteLine(result);
}
谁能指出一些资源和/或告诉我安装哪个nuget包来实现这一点。我已经安装了微软软件。代码分析,但不能弄清楚是否可行,我觉得我错过了一些东西。
在最新的预览版中(暂时)删除了允许您非常容易地做到这一点的脚本api。您仍然可以编译脚本,发出和加载程序集,并通过执行
一行来调用其入口点。public static class Program
{
public static void Main(string[] args)
{
var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
var defaultReferences = new[] { "mscorlib.dll", "System.dll", "System.Core.dll" };
var script = @"using System;
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine(""Hello {0}"", args[0]);
}
}";
// Parse the script to a SyntaxTree
var syntaxTree = CSharpSyntaxTree.ParseText(script);
// Compile the SyntaxTree to a CSharpCompilation
var compilation = CSharpCompilation.Create("Script",
new[] { syntaxTree },
defaultReferences.Select(x => new MetadataFileReference(Path.Combine(assemblyPath, x))),
new CSharpCompilationOptions(OutputKind.ConsoleApplication));
using (var outputStream = new MemoryStream())
using (var pdbStream = new MemoryStream())
{
// Emit assembly to streams.
var result = compilation.Emit(outputStream, pdbStream: pdbStream);
if (!result.Success)
{
return;
}
// Load the emitted assembly.
var assembly = Assembly.Load(outputStream.ToArray(), pdbStream.ToArray());
// Invoke the entry point.
assembly.EntryPoint.Invoke(null, new object[] { new[] { "Tomas" } });
}
}
}
它将在控制台中输出Hello Tomas
:)
似乎在2014年4月的版本中,脚本已被暂时删除:
REPL和托管脚本api发生了什么?
团队正在审查这些组件的设计之前的ctp,然后再重新引入组件。目前该团队正在努力完成的语言语义互动/脚本代码。