发布于 2026-01-06 0 阅读
0

使用 dotnet-script 的 C# 脚本

使用 dotnet-script 的 C# 脚本

你知道 Python 用户可以直接在文件中编写代码.py并运行它python3 filename.py,而无需像 C# 用户那样经历“新建项目”和“启动public static void main”等繁琐的步骤吗?C# 也可以编写脚本——这是我大约一年前才学到的。

Roslyn 让 C# 脚本编写成为可能。如果您感兴趣,可以阅读Mark Michaelis于 2016 年 1 月发表在 MSDN 上的题为“C# 脚本编写”的博文。

我真正着迷的工具是dotnet-script,它是一个跨平台的 .NET Core 全局工具,通过 omnisharp 在 VS Code 上提供完整的智能感知支持,涵盖了您的大部分实验需求。

先决条件

  1. .NET Core SDK 2.1+
  2. VS Code
  3. VS Code 官方 C# 扩展

安装

首先请确保您已从dot.net安装最新版本的 .NET Core SDK 。最低要求是 .NET Core 2.1。

最简单的安装方法dotnet-script是将其安装为全局工具:

dotnet tool install --global dotnet-script

Enter fullscreen mode Exit fullscreen mode

现在你应该可以运行该程序了dotnet script --version,它应该会打印出该工具的版本信息dotnet-script

你好世界

  1. 创建一个新目录来存储所有脚本。
  2. 初始化一个新脚本。
  3. 跑步。
mkdir console
cd console
dotnet script init hello
dotnet script hello.csx

Enter fullscreen mode Exit fullscreen mode
  • init 命令会创建一个.csx(C# 脚本)文件,以及一个用于智能感知的文件和一个用于调试支持的omnisharp.jsonVS Code文件。launch.json
  • csx 脚本使用以下命令运行dotnet script filename

生成的hello.csx文件包含以下内容:

#!/usr/bin/env dotnet-script

Console.WriteLine("Hello world!");

Enter fullscreen mode Exit fullscreen mode

第一行是 *nix 用户应该很熟悉的 shebang。这意味着我们可以直接运行它./hello.csx,它就能正常工作!(在 Windows 上也有效,但你需要.csx先使用 `.` 将文件与 dotnet-script关联起来dotnet script register。)

另一个简单的例子

控制台应用程序的一个典型用例是尝试新的库,它dotnet-script是完成这项工作的完美工具。假设我正在学习Stateless库,以下是我的实验步骤:

  1. 使用以下方式创建一个新脚本dotnet script init stateless
  2. code .在 VS Code 中使用.打开文件夹
  3. 使用 roslyn 指令导入 nuget 包#r
  4. 访问他们的 GitHub 页面,复制一些示例代码。
  5. 如果需要,可以使用 using Cmd+.(或Ctrl+.)。
  6. 在 VS Code 中使用 F5 进行调试。

这是我的stateless.csx文件:

#!/usr/bin/env dotnet-script
#r "nuget: Stateless, 4.2.1"

// Copied from: https://github.com/dotnet-state-machine/stateless/blob/dev/example/OnOffExample/Program.cs
using Stateless;

const string on = "On";
const string off = "Off";
const char space = ' ';

// Instantiate a new state machine in the 'off' state
var onOffSwitch = new StateMachine<string, char>(off);

// Configure state machine with the Configure method, supplying the state to be configured as a parameter
onOffSwitch.Configure(off).Permit(space, on);
onOffSwitch.Configure(on).Permit(space, off);

Console.WriteLine("Press <space> to toggle the switch. Any other key will exit the program.");

while (true)
{
    Console.WriteLine("Switch is in state: " + onOffSwitch.State);
    var pressed = Console.ReadKey(true).KeyChar;

    // Check if user wants to exit
    if (pressed != space) break;

    // Use the Fire method with the trigger as payload to supply the state machine with an event.
    // The state machine will react according to its configuration.
    onOffSwitch.Fire(pressed);
}

Enter fullscreen mode Exit fullscreen mode

给刚开始使用 VS Code 进行开发的用户的注意事项:

  • 如果执行第 3 步后智能感知功能无法正常工作,或者执行第 5 步后没有代码操作可用,则可能需要通过以下方式重启 OmniSharp:Cmd+Shift+P--> Omnisharp: Restart Omnisharp有关更多信息,请参阅filipw/dotnet-script#424 。
  • 运行应用程序时,您很可能会看到以下错误:“当应用程序没有控制台或控制台输入被重定向时,无法读取按键。”这是因为 VS Code 默认使用调试控制台,而调试控制台不支持控制台输入。您可以通过配置 VS Code 改用集成终端来更改此行为,方法是向 VS Code 的配置文件"console": "integratedTerminal"中添加一个属性.vscode/launch.json。更多信息请参见此处

就是这样。依我看,这设计相当巧妙。


我喜欢的dotnet-script是:

  1. 所有相关代码,包括所需的 NuGet 包,都可以存在于单个文件中。
  2. 全面可靠的智能感知。
  3. 支持调试器。
  4. 没有public static void main.csproj文件。
文章来源:https://dev.to/galdin/c-scripts-using-dotnet-script-3b1k