Mykeels.CSharpRepl 0.0.3

There is a newer prerelease version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mykeels.CSharpRepl" Version="0.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mykeels.CSharpRepl" Version="0.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Mykeels.CSharpRepl" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mykeels.CSharpRepl --version 0.0.3
                    
#r "nuget: Mykeels.CSharpRepl, 0.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Mykeels.CSharpRepl&version=0.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Mykeels.CSharpRepl&version=0.0.3
                    
Install as a Cake Tool

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 load
  • usings: Array of namespaces to import by default
  • applicationName: Name of your application
  • logSuccess: Callback for handling successful evaluations
  • logError: Callback for handling evaluation errors
  • commands: 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

  1. Assembly References: Include all necessary assemblies in the references array
  2. Namespaces: Add commonly used namespaces to the usings array
  3. Error Handling: Implement custom error handling in logError for better debugging
  4. Output Formatting: Use AnsiConsole for colored output and better readability
  5. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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