Mykeels.CSharpRepl
0.0.3
See the version list below for details.
dotnet add package Mykeels.CSharpRepl --version 0.0.3
NuGet\Install-Package Mykeels.CSharpRepl -Version 0.0.3
<PackageReference Include="Mykeels.CSharpRepl" Version="0.0.3" />
<PackageVersion Include="Mykeels.CSharpRepl" Version="0.0.3" />
<PackageReference Include="Mykeels.CSharpRepl" />
paket add Mykeels.CSharpRepl --version 0.0.3
#r "nuget: Mykeels.CSharpRepl, 0.0.3"
#addin nuget:?package=Mykeels.CSharpRepl&version=0.0.3
#tool nuget:?package=Mykeels.CSharpRepl&version=0.0.3
Mykeels.CSharpRepl
This library is a stripped-down, plug-n-play version of CSharpRepl.Services. It is a powerful C# REPL (Read-Eval-Print Loop) that can be embedded into any .NET application, providing an interactive C# environment with syntax highlighting, code completion, and more.
Installation
dotnet add package Mykeels.CSharpRepl
Quick Start
Here's a minimal example of how to use Mykeels.CSharpRepl in your application:
using Mykeels.CSharpRepl;
await Repl.Run();
This will start an interactive C# REPL with default settings.
Configuration
You can customize the REPL by providing a Configuration
object:
using CSharpRepl.Services;
using Mykeels.CSharpRepl;
using Spectre.Console;
await Repl.Run(
new Configuration(
// Add references to assemblies
references: AppDomain
.CurrentDomain.GetAssemblies()
.Select(a => $"{a.GetName().Name}.dll")
.ToArray(),
// Add default namespaces
usings: [
"System",
"System.Collections.Generic",
"System.IO",
"System.Linq",
"System.Net.Http",
"System.Threading",
"System.Threading.Tasks"
],
// Set application name
applicationName: "MyApp.CSharpRepl",
// Customize success output
logSuccess: (message, result) => {
Console.WriteLine($"<< {message}");
string output = Newtonsoft.Json.JsonConvert.SerializeObject(result);
AnsiConsole.MarkupLine($"[green]>> {output}[/]");
},
// Customize error output
logError: (message, exception, _) => {
Console.WriteLine($"<< {message}");
string output = Newtonsoft.Json.JsonConvert.SerializeObject(
new { Error = exception }
);
Console.WriteLine($">> {output}");
AnsiConsole.MarkupLine($"[red]>> {output.EscapeMarkup()}[/]");
}
)
);
Pre-execution Commands
You can specify commands to be executed before the REPL starts. This is useful for setting up the environment or importing commonly used types:
await Repl.Run(
commands: [
// Import ScriptGlobals to make its methods available directly
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
]
);
ScriptGlobals
You can add your own ScriptGlobals by adding a static class with static methods and properties, and then running a pre-execution command on REPL startup.
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;"
Features
- Syntax Highlighting: Code is colorized for better readability
- Code Completion: Intelligent code completion with IntelliSense
- Error Handling: Detailed error messages with stack traces
- JSON Output: Results are automatically serialized to JSON
- Customizable: Configure references, namespaces, and output formatting
- Interactive: Full C# interactive environment with REPL capabilities
Configuration Options
The Configuration
class supports the following options:
references
: Array of assembly references to loadusings
: Array of namespaces to import by defaultapplicationName
: Name of your applicationlogSuccess
: Callback for handling successful evaluationslogError
: Callback for handling evaluation errorscommands
: Array of commands to execute before starting the REPL
Examples
Basic Usage
await Repl.Run();
With Custom References
await Repl.Run(
new Configuration(
references: ["MyApp.dll", "MyApp.Models.dll"]
)
);
With Custom Output Formatting
await Repl.Run(
new Configuration(
logSuccess: (message, result) => {
Console.WriteLine($"Input: {message}");
Console.WriteLine($"Result: {result}");
}
)
);
With Pre-execution Commands
await Repl.Run(
commands: [
"using static Mykeels.CSharpRepl.Sample.ScriptGlobals;",
"var greeting = \"Hello, World!\";"
]
);
Best Practices
- Assembly References: Include all necessary assemblies in the
references
array - Namespaces: Add commonly used namespaces to the
usings
array - Error Handling: Implement custom error handling in
logError
for better debugging - Output Formatting: Use
AnsiConsole
for colored output and better readability - Pre-execution Commands: Use
commands
to set up your environment and import commonly used types
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Ben.Demystifier (>= 0.4.1)
- ICSharpCode.Decompiler (>= 8.2.0.7535)
- Microsoft.Build.Locator (>= 1.7.8)
- Microsoft.CodeAnalysis.CSharp.Features (>= 4.11.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.11.0)
- Microsoft.CodeAnalysis.Workspaces.MSBuild (>= 4.11.0)
- Microsoft.Extensions.Caching.Memory (>= 9.0.0)
- Microsoft.Extensions.DependencyModel (>= 9.0.0)
- Microsoft.SymbolStore (>= 1.0.555801)
- PrettyPrompt (>= 4.1.1)
- Spectre.Console.Cli (>= 0.49.1)
- System.Configuration.ConfigurationManager (>= 9.0.0)
- System.IO.Abstractions (>= 21.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
0.0.4-alpha.1031060f | 200 | 2 months ago |
0.0.4-alpha.10 | 200 | 2 months ago |
0.0.3 | 153 | 2 months ago |
0.0.3-alpha.5290079a | 191 | 2 months ago |
0.0.2 | 141 | 2 months ago |
0.0.1 | 141 | 2 months ago |
0.0.0 | 138 | 2 months ago |