百科.dev
全部条目AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

CSharpRepl

> 开发工具
开源

一个带有语法高亮的命令行 C# REPL - 以交互方式探索语言、库和 NuGet 包。

3.3K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个带有语法高亮的命令行 C# REPL - 以交互方式探索语言、库和 NuGet 包。

# C# REPL A cross-platform command line REPL for the rapid experimentation and exploration of C#. It supports intellisense, installing NuGet packages, and referencing local .NET projects and assemblies. C# REPL provides the following features: - Syntax highlighting via ANSI escape sequences - Intellisense with documentation and overload navigation - Automatic formatting of typed input - Nuget package installation - Reference local assemblies, solutions, and projects - Dump and explore objects with syntax highlighting and rich Spectre.Console formatting - Connect to a running .NET application and run the REPL inside that application, with access to application state and the ability to replace live methods - Navigate to source via Source Link - IL disassembly and "lowered" C# decompilation (both Debug and Release mode, using ILSpy) - AI code completion via OpenAI, Anthropic, Gemini, Grok, DeepSeek, Mistral/Codestral, or any other OpenAI-compatible provider (bring your own API key) - Fast and flicker-free rendering. A "diff" algorithm is used to only render what's changed. ## Installation C# REPL is a .NET 10 global tool, and runs on Windows, Mac OS, and Linux. It can be installed [from NuGet](https://www.nuget.org/packages/CSharpRepl) via: ```console dotnet tool install -g csharprepl ``` If you're running on Mac OS Catalina (10.15) or later, make sure you follow any additional directions printed to the screen. You may need to update your PATH variable in order to use .NET global tools. After installation is complete, run `csharprepl` to begin. You can update C# REPL by running `dotnet tool update -g csharprepl`. ## Themes and Colors The default theme uses the same colors as Visual Studio dark mode, and custom themes can be created using a [`theme.json`](https://github.com/waf/CSharpRepl/blob/main/CSharpRepl/themes/dracula.json) file. Additionally, your terminal's colors can be used by supplying the `--useTerminalPaletteTheme` command line option. To completely disable colors, set the NO_COLOR environment variable. ## Usage Type some C# into the prompt and press Enter to run it. The result, if any, will be printed: ```csharp > Console.WriteLine("Hello World") Hello World > DateTime.Now.AddDays(8) [6/7/2021 5:13:00 PM] ``` To evaluate multiple lines of code, use Shift+Enter to insert a newline: ```csharp > var x = 5; var y = 8; x * y 40 ``` Additionally, if the statement is not a "complete statement" a newline will automatically be inserted when Enter is pressed. For example, in the below code, the first line is not a syntactically complete statement, so when we press enter we'll go down to a new line: ```csharp > if (x == 5) | // caret position, after we press Enter on Line 1 ``` Finally, pressing Ctrl+Enter will show a "detailed view" of the result. For example, for the `DateTime.Now` expression below, on the first line we pressed Enter, and on the second line we pressed Ctrl+Enter to view more detailed output: ```csharp > DateTime.Now // Pressing Enter shows a reasonable representation [5/30/2021 5:13:00 PM] > DateTime.Now // Pressing Ctrl+Enter shows a detailed representation [5/30/2021 5:13:00 PM] { Date: [5/30/2021 12:00:00 AM], Day: 30, DayOfWeek: Sunday, DayOfYear: 150, Hour: 17, InternalKind: 9223372036854775808, InternalTicks: 637579915804530992, Kind: Local, Millisecond: 453, Minute: 13, Month: 5, Second: 0, Ticks: 637579915804530992, TimeOfDay: [17:13:00.4530992], Year: 2021, _dateData: 9860951952659306800 } ``` **A note on semicolons**: C# expressions do not require semicolons, but [statements](https://stackoverflow.com/questions/19132/expression-versus-statement) do. If a statement is missing a required semicolon, a newline will be added instead of trying to run the syntatically incomplete statement; simply type the semicolon to complete the statement. ```csharp > var now = DateTime.Now; // assignment statement, semicolon required > DateTime.Now.AddDays(8) // expression, we don't need a semicolon [6/7/2021 5:03:05 PM] ``` When you're done with your session, you can type `exit` or press Ctrl+D to exit. ## Adding References Use the `#r` command to add assembly or nuget references. - For assembly references, run `#r "AssemblyName"` or `#r "path/to/assembly.dll"` - For project references, run `#r "path/to/project.csproj"`. Solution files (`.sln` and `.slnx`) can also be referenced. - For nuget references, run `#r "nuget: PackageName"` to install the latest version of a package, or `#r "nuget: PackageName, 13.0.5"` to install a specific version (13.0.5 in this case).

To run ASP.NET applications inside the REPL, start the `csharprepl ` application with the `--framework` parameter, specifying the `Microsoft.AspNetCore.App` shared framework. Then, use the above `#r` command to reference the application DLL. See [Configuring CSharpRepl](https://github.com/waf/CSharpRepl/wiki/Configuring-CSharpRepl) for more details. ```console csharprepl --framework Microsoft.AspNetCore.App ``` ## Loading scripts Use the `#load` directive to run a C# script file (`.csx`), e.g. `#load "path/to/script.csx"`. This is handy for initializing a session, as any references, namespaces, and variables the script defines remain available afterwards. ## Connecting to a running process In addition to the normal REPL, which evaluates code in csharprepl's own process, csharprepl can attach to other .NET applications and evaluate expressions inside them, reading and writing live application state (e.g. statics and services resolved from DI). > [!WARNING] > Connecting to a connector-enabled process is **equivalent to running arbitrary code inside it, with its privileges**. This is a development and diagnostics tool; never enable the connector on a production process. CSharpRepl injects a real Roslyn scripting engine into the target application, so you can run unconstrained C# in that application. This is not a debugger; breakpoints, stepping, and non-cooperative attach are not supported. The target's source does not need to be modified, but the application must "opt in" by running from a shell with two special environment variables that allow CSharpRepl to inject the REPL: 1. Print the environment variables to launch your app with: ```console csharprepl connect init # this autodetects your shell, or pass e.g. --shell pwsh ``` 2. Set the environment variables from the previous step, and launch your app in that shell. You should NOT set these as permanent environment variables on your machine. Only set them in the shell where you plan to launch the target application. 3. Attach to the application by its process ID: ```console csharprepl connect list # lists available processes to attach to, with their process ID. csharprepl connect 1234 # attaches to a process with process ID e.g. 1234 ``` This will start the REPL in the target application. Some things you can try: - Statics: reference them by their fully-qualified name, e.g. `MyApp.Program.SomeStatic` (read and write). - DI services (ASP.NET Core or Generic Host apps): `services.GetRequiredService()` or the shorthand `Get()`. The connector captures the application's root service provider via .NET's hosting hooks. Type `exit` (or press Ctrl+D) to detach. The target application will keep running, and you can reconnect to it later. ### Modifying a running process While connected to a process, you can also replace live methods in that process. Define a method matching the target's signature. Instance methods take the instance as the first parameter: ```csharp > decimal half(MyApp.OrderService svc, int qty, decimal unit) => qty * unit * 0.5m; ``` Then run the following (with the fully qualified target method name) to replace the original method: ```csharp #replace MyApp.OrderService.CalculatePrice with half ``` To wrap a method instead of replacing it, define a method whose first parameter is an `orig` delegate that calls the original: ```csharp > decimal logged(Func orig, MyApp.OrderService svc, int qty, decimal unit) { var price = orig(svc, qty, unit); Console.WriteLine($"CalculatePrice({qty}, {unit}) = {price}"); return price; } > #wrap MyApp.OrderService.CalculatePrice with logged ``` To undo a modification, use `#patches` to list active patches and `#revert ` or `#revert all` to undo them. Patches take effect immediately and persist in the target until reverted or the process exits. Patching is done via the excellent [MonoMod](https://github.com/monomod/monomod) library **Requirements and limitations:** - `net10.0` targets only. The connector and the target must both be on .NET 10. - Apps published as single-files have very limited functionality: - A framework-dependent single-file app's assemblies are bundled with no metadata, so strongly-typed access to the app's own types is unavailable. You need to use reflection to access the app's types. - A self-contained single-file app is unsupported (even the runtime is bundled, so nothing can be compiled). The connector will refuse to start. - Method replacement is not supported for generic methods, pointer parameters, and methods the JIT already inlined at a call site. See the [Injected Hook documentation](https://github.com/waf/CSharpRepl/blob/main/InjectedHook/InjectedHookReadme.md) for information on how this works under the hood. ## AI Code Completion C# REPL can suggest completions using an AI model. Press Ctrl+Alt+Space at the caret to request a completion; the generated code streams directly into the prompt at the caret as it arrives. Suggestions are generated from the code you've typed in the current session, so they're aware of the variables, methods, and types you've already defined.

This works with OpenAI, Anthropic, Gemini, Grok, DeepSeek, Mistral/Codestral, or any other OpenAI-compatible provider (bring your own API key). Use the `--aiProvider` option and related settings to choose and/or configure a provider; see [Configuring CSharpRepl](https://github.com/waf/CSharpRepl/wiki/Configuring-CSharpRepl) for details. ## Keyboard Shortcuts CSharpRepl aims for a similar editing experience as Visual Studio (e.g. for text navigation, selection and keyboard shortcuts). - **Basic Usage** - Ctrl+C - Cancel current line (or copies text if text is highlighted) - Ctrl+D or type `exit` - Exit the REPL - Ctrl+L or type `clear` - Clear screen - Enter - Evaluate the current line if it's a syntactically complete statement; otherwise add a newline - Ctrl+Enter or Ctrl+Alt+Enter - Evaluate the current line, and return a more detailed representation of the result - Shift+Enter or Alt+Enter - Insert a new line without evaluating - Ctrl+Z / Ctrl+Y - Undo / redo - Ctrl+Alt+Space - Request an AI code completion at the caret (requires an AI provider API key to be configured; OpenAI by default, see `--aiProvider`) - **Editing & Clipboard** - Ctrl+Shift+C - Copy the entire current input to the clipboard - Ctrl+X - Cut the highlighted text, or the current line if nothing is highlighted - Shift+Delete - Cut the current line - Ctrl+V, Shift+Insert, and Ctrl+Shift+V - Paste text to prompt. Automatically trims leading indent - Ctrl+A - Select all - Ctrl+Backspace / Ctrl+Delete - Delete the word to the left / right of the caret - Ctr

GitHub Issues· 32 开放

在 GitHub 查看全部
  • #514

    Nested references inside #load inside a .csx file don't resolve

    bug更新于 2026年8月12日
  • #513

    Intellisense failure with multi-line code blocks

    bug更新于 2026年7月23日
  • #30

    Remote Console For Web Applications

    enhancement更新于 2026年6月25日
  • #489

    Unable to use the new extension members

    bug更新于 2026年6月19日
  • #453

    Add an Option to hide the welcome notice

    enhancement更新于 2026年6月6日
  • #445

    File completion

    enhancement更新于 2026年2月11日
  • #401

    Show result of last statement

    enhancement更新于 2025年6月28日
  • #398

    Trying to use it in Godot Engine and some feature requests

    enhancement更新于 2025年6月1日
  • #393

    Make a C# repl for your project in 2 minutes

    enhancement更新于 2025年1月28日
  • #390

    How to use as a replacement for the C# Interactive tool in JetBrains Rider?

    enhancement更新于 2024年12月19日

核心特点

  • •Syntax highlighting via ANSI escape sequences
  • •Intellisense with documentation and overload navigation
  • •Automatic formatting of typed input
  • •Nuget package installation
  • •Reference local assemblies, solutions, and projects
  • •Dump and explore objects with syntax highlighting and rich Spectre.Console formatting
  • •Connect to a running .NET application and run the REPL inside that application, with access to application state and the ability to replace live methods
  • •Navigate to source via Source Link
  • •IL disassembly and "lowered" C# decompilation (both Debug and Release mode, using ILSpy)
  • •AI code completion via OpenAI, Anthropic, Gemini, Grok, DeepSeek, Mistral/Codestral, or any other OpenAI-compatible provider (bring your own API key)

> 标签

C#cliconsolecsharpdotnet

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类开发工具
定价开源

> 相关工具

V
VS Code
流行的开源代码编辑器
G
Git
分布式版本控制系统
V
Vite
下一代前端构建工具