Skip to content

A lightweight C# script engine for dynamic execution with global variables, namespace imports, and assembly references.

License

Notifications You must be signed in to change notification settings

xukonxe/CsharpScript.Running

Repository files navigation

CsharpScript.Running

Links

Description

This repository contains a lightweight C# script execution engine built on top of the Microsoft.CodeAnalysis.CSharp.Scripting library. It allows developers to dynamically execute C# scripts with support for:

  • Global Variables: Set global context for scripts with user-defined objects.
  • Dynamic Imports: Add namespaces such as System.Linq and others at runtime.
  • Assembly References: Dynamically reference assemblies during script execution.
  • Script Evaluation: Run C# scripts with both void and return types.
  • Logging Support: Easily log messages, warnings, and errors through extension methods.

Key Features

  • Set Global Variables: Pass an object with fields that can be accessed and modified within the script.
  • Namespace Imports: Add commonly used namespaces dynamically, such as System.Linq.
  • Assembly References: Reference assemblies at runtime to unlock additional functionality.
  • Typed Script Execution: Execute scripts and return results with a specific type.
  • Logging: Built-in logging methods to output logs, warnings, and errors to the console for easier debugging.

Example Usage

// Globals is a custom class, and the system automatically parses its fields and values into the script's global variables.
public class Globals {
    public string name = "undefined";
    public int age = 0;
}

new CSharpScriptRunning()
    .SetGlobals(new Globals())
    .Import("System.Linq")
    .Reference(typeof(Enumerable).Assembly)
    .Run<int>("age = 10; return age;")
    .Log();

This example demonstrates setting a global variable, importing namespaces, adding assembly references, and running a script with a return type.

Feel free to explore the repository and contribute!

About

A lightweight C# script engine for dynamic execution with global variables, namespace imports, and assembly references.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages