Skip to content
oleg-shilo edited this page Mar 18, 2016 · 39 revisions
# CS-Script

This is the source code repository of the original CS-Script.

Currently it is used world wide for extending the applications functionality with scripting and as a general purpose scripting environment. It is used by both enthusiasts and by professional programmers. It found its way to non-profit organizations (e.g. educational institutes) as well as to commercial organizations. These are just a few examples: MediaPortal, FlashDevelop, K2 API, SF.net ("WinTin"), BonSAI, AyaNova (service management software)...

In 15 April 2014 (10 years after the first public release) CS-Script has been re-released under MIT license and since then its source code is hosted by this site (CodePlex Git). At the time of creating this repository at CodePlex (first OS hosting) CS-Script has been downloaded 239,477 times world wide. And at the time of the first GitHub release the CodePlex downloads have reached 28,239.

At this site you will find:

  • Source code and releases
  • Top-level documentation

For the in-depth online API reference documentation please visit CS-Script Home Page.

This documentation will provide a complete overview of CS-Script functionality but few code snippets below can give you a quick idea what CS-Script is about:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System.Windows.Forms;
                                     public class Script
                                     {
                                         public void SayHello(string greeting)
                                         {
                                             MessageBox.Show(""Greeting: ""+ greeting);
                                         }
                                     }");
script.SayHello("Hello World!");
var sqr = await CSScript.Evaluator
                        .CreateDelegateAsync(@"int Sqr(int a)
                                               {
                                                   return a * a;
                                               }");
OutputPanel.Print(sqr(3));
var sum = CSScript.Evaluator
                  .CreateDelegateRemotely<int>(@"int Sum(int a, int b)
                                                 {
                                                     return a+b;
                                                 }");

int result = sum(15, 3);

sum.UnloadOwnerDomain();
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" , typeof(string)); 
SayHello("Hello again!");

CS-Script can execute script files without an additional hosting solution. Just from the command prompt:

C:\> cscs ss_host.cs

Where ss_host.cs is a script for hosting ServiceStack REST server:

//css_npp asadmin;
//css_inc ServiceStack.cs;
using System;
using ServiceStack;

public class Host : ConsoleHost
{
    static void Main()
    {
        new Host().Init()
                  .Start("http://*:8080/");

        Console.WriteLine(@"AppHost listening on http://localhost:8080/
                            sample: http://localhost:8080/hello/John");
        Console.ReadKey();
    }
}

[Route("/hello/{Name}")]
public class Hello
{
    public string Name { get; set; }
}

public class StatusService : Service
{
    public object Get(Hello request)
    {
        return new
        {
            Message = $"Hello {request.Name}!",
            Timestamp = DateTime.Now.ToString()
        };
    }
}

You can also use take an advantage of CS-Script dedicated fully featured IDE (Intellisense, execution, debugging, packaging) built on top of Notepad++.

How to build binaries
Source code includes \Build\build.cmd, which builds all CS-Script binaries.

It also includes VS2015 solution for building script engine executable and the class library assemblies to allow more convenient debugging, troubleshooting.

How to install CS-Script on Windows
You can download the package from the Downloads page and deploy the package content manually. Alternatively you can use Chocolatey.

Chocolatey is the repository for the Windows software packages. Chocolatey NuGet is a Machine Package Manager, somewhat like apt-get, but built with Windows in mind. It is also the source repository of the OneGet package manager of Windows 10.

CS-Script can be installed with the following command:

C:\> choco install cs-script

Read more on Chocolatey and how to enable it on your version of windows here: https://chocolatey.org/

Note, by default support for C# 6.0 is disabled. Instructions on how to enable it can be found here: C#6.0 support

How to host script engine in the application

You can use Visual Studio console application project and NuGet package as the starting point.

PM> Install-Package CS-Script

After installing the package the VS project will include the scripting.cs file containing the samples for all supported hosting scenarios.