# cs-script **Repository Path**: pigeon79/cs-script ## Basic Information - **Project Name**: cs-script - **Description**: 用C#语言去做脚本语言,速度甩Python N条街!而且可以使用.NET全部类库,支持MONO!强烈适合患有“强迫症”的程序猿使用。不像PYthon那样无类型、无定义声明!让你写脚本写的踏实放心!!此代码fork自github! - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 50 - **Created**: 2024-12-24 - **Last Updated**: 2024-12-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # CS-Script [![Build status](https://ci.appveyor.com/api/projects/status/jruj9dmf2dwjn5p3?svg=true)](https://ci.appveyor.com/project/oleg-shilo/cs-script) [![Chocolatey Version](http://img.shields.io/chocolatey/v/cs-script.svg?style=flat-square)](http://chocolatey.org/packages/cs-script) [![Chocolatey Downloads](http://img.shields.io/chocolatey/dt/cs-script.svg?style=flat-square)](http://chocolatey.org/packages/cs-script) [![NuGet version (CS-Script)](https://img.shields.io/nuget/v/CS-Script.svg?style=flat-square)](https://www.nuget.org/packages/CS-Script/) [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://oleg-shilo.github.io/cs-script/Donation.html) CS-Script is a CLR based scripting system which uses ECMA-compliant C# as a programming language. CS-Script is one of the most mature C# scripting solutions. It became publicly available in 2004, just two years after the first release of .NET. And it was the first comprehensive scripting platform for .NET CS-Script supports both hosted and standalone execution model. This makes it possible to use the script engine as a pure C# alternative for PowerShell. As well as extending .NET applications with C# scripts executed at runtime by the hosted script engine. CS-Script allows seamlessly switching underlying compiling technology without affecting the code base. Currently supported compilers are Mono, Roslyn and CodeDOM. CS-Script also offers comprehensive integration with most common development tools: - [Visual Studio](https://github.com/oleg-shilo/CS-Script.VSIX), - [VSCode](https://github.com/oleg-shilo/cs-script.vscode), - [Sublime Text 3](https://github.com/oleg-shilo/cs-script-sublime), - [Notepad++](https://github.com/oleg-shilo/cs-script.npp). It can be run on Win, Linux and Mac. And it is compatible with .NET, Mono and .NET Core. Over the long history of CS-Script it has been downloaded through Notepad++ x86 plugin manager alone over ![](https://oleg-shilo.github.io/cs-script/download.stats/css.npp.count.jpeg) times ([see stats](https://www.cs-script.net/cs-script/download.stats/daily_downloads.html)). \* _statistics does not include x64 downloads nor downloads after Notepad++ discontinued shiping editor with the plugin manager x86 included_ _**For the all CS-Script details go to the project [Documentation Wiki](https://github.com/oleg-shilo/cs-script/wiki).**_
The following is a simple code sample just to give you the idea about the product: _**Executing script from shell**_ Updating media file tags. Note, the script is using optional classless layout. _Script file: `mp4_retag.cs`_ ```C# //css_nuget taglib using System; using System.IO; string source = @"\\media-server\tv_shows\Get Smart\Season1"; void main() { foreach (string file in Directory.GetFiles(source, "*.mp4")) { string episode_name = Path.GetFileNameWithoutExtension(file); var mp4 = TagLib.File.Create(file); mp4.Tag.Title = episode_name; mp4.Save(); Console.WriteLine(episode_name); } } ``` Execute script file directly in cmd-prompt without building an executable assembly: ``` C:\Temp>cscs mp4_retag.cs ``` _**Hosting script engine**_ ```C# dynamic script = CSScript.LoadCode( @"using System.Windows.Forms; public class Script { public void SayHello(string greeting) { MessageBox.Show(""Greeting: "" + greeting); } }") .CreateObject("*"); script.SayHello("Hello World!"); //----------------- var product = CSScript.CreateFunc(@"int Product(int a, int b) { return a * b; }"); int result = product(3, 4); //----------------- var SayHello = CSScript.LoadMethod( @"using System.Windows.Forms; public static void SayHello(string greeting) { MessageBoxSayHello(greeting); ConsoleSayHello(greeting); } static void MessageBoxSayHello(string greeting) { MessageBox.Show(greeting); } static void ConsoleSayHello(string greeting) { Console.WriteLine(greeting); }") .GetStaticMethod("*.SayHello" , ""); SayHello("Hello again!"); ``` Hosting script engine You can host the script engine in any .NET application. The class library is distributed as a NuGet package CS-Script. Install-Package CS-Script The library is built against .NET Standard 2.0 so it can be hosted on any edition of runtime. However the script evaluation is done via .NET 5 tool chain so it needs to be installed on the host PC even if the application is implemented with the older framework (e.g. .NET Framework). These are just a few samples: The complete content of samples can be found here. ``` public interface ICalc { int Sum(int a, int b); } ``` // you can but don't have to inherit your script class from ICalc ICalc calc = CSScript.Evaluator .LoadCode(@"using System; public class Script { public int Sum(int a, int b) { return a+b; } }"); int result = calc.Sum(1, 2); dynamic script = CSScript.Evaluator .LoadMethod(@"int Product(int a, int b) { return a * b; }"); int result = script.Product(3, 2); public interface ICalc { int Sum(int a, int b); int Div(int a, int b); } ``` ICalc script = CSScript.Evaluator .LoadMethod(@"public int Sum(int a, int b) { return a + b; } public int Div(int a, int b) { return a/b; }"); int result = script.Div(15, 3); var log = CSScript.Evaluator .CreateDelegate(@"void Log(string message) { Console.WriteLine(message); }"); log("Test message"); int sum = CSScript.Evaluator.Eval("6 + 3"); ```