Quantcast
Channel: CS-Script Source
Viewing all 355 articles
Browse latest View live

Updated Wiki: Standalone Script Execution

$
0
0
Further reading:

Script stand alone execution

Script stand alone execution is the most mature and stable part of CS-Script feature set. Thus the major online documentation has been written quite some time ago and while it may currently have some referencing discrepancies (e.g. VS2012 instead of VS2015) it is still an accurate primary source of CS-Script knowledge. This Wiki article on the other hand is the most concise and concentrated description of the relevant content and in many cases it will be fully sufficient for majority of the readers. In all other cases please refer to the primary Documentation online.

CS-Script allows a direct single step execution of files containing ECMA-compliant C# code. The script engine executable comes in three forms:
  • cscs.exe - console application executable
  • csws.exe - Windows application executable
  • css.exe - Universal executable. It launches cscs.exe and makes console window visible on the first console output from the script being executed.
The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter.
Thus the minimal deployment file set for CS-Script is either cscs.exe or csws.exe file. Any of these files is sufficient for the wast majority of the scripting scenarios.

Installing CS-Script environment

The simplest way to start using CS-Script is to install it from Chocolatey, which is a repository for the Windows software packages. Chocolatey it is a Windows version of Linux apt-get. CS-Script can be installed with the following command:
C:\> choco install cs-script
During the installation Chocolatey downloads the latest version of CS-Script (from the official releases repository), extracts all files in the default Chocolatey libraries location and executes CS-Script's cssconfig.exe, which configures the OS for running C# scripts. Strictly speaking running cssconfig.exe is optional. The only thing that it does is adding the CS-Script location to the system PATH, and adds Windows Explorer shell extensions (context menu).

Deploying C# scripts

Any OS that have .NET/Mono installed can run C# scripts. The minimal distribution file set is the script engine executable and the script file itself. Of course if the script depends on the third-party library or other scripts then they also need to be deployed on the target system. The easiest way of preparing the script for running on another system is to use Notepad++ with C# Intellisense plugin enabled. Just click the "distro" button and the plugin will analyse the script and package it with all its dependencies along with the script engine itself. Optionally it may convert the script into a self sufficient single executable.

Features

From the start CS-Script was heavily influenced by Python and the developer experiences it delivers. Thus the it tries to match the most useful Python features (apart from the Python syntax). Here are some highlight of the CS-Script features. Some more info can be found here.
  • Scripts are written in 'plain vanilla' CLS-compliant C#. Though classless scripts are also supported.
  • Remarkable execution speed matching performance of compiled managed applications.
  • Including (referencing) dependency scripts from the main script.
  • Referencing external assemblies from the script.
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings').
  • Automatic resolving (downloading and referencing) NuGet packages.
  • Building a self sufficient executable from the script.
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script plugin for Notepad++ brings true Intellisense to the 'peoples editor')

Quick guide of CS-Script directives

CS-Script relies on so called 'script directives' for implementing some features. These directives are the C# comments that do not abstract standard C# compilers and yet provide user defined instructions for the script engine. Set of supported directives is very small set of commend. The full description with code samples can be found here. The following is a cheat sheet on script directives:
url//css_inc - import/include another C# script (similar to C++ #include).
url//css_ref - reference .NET assembly.
url//css_args - provide startup arguments.
url//css_dir - set additional probing directories for dependency scripts and assemblies.
url//css_nuget - reference (and download if not present) NuGet package.
Note, due to the fact that Microsoft stopped distributing the latest C# compiler with it's .NET deployment, support for C# 6.0 syntax requires one extra step to be done. You will need to indicate the location of the Roslyn compiler (deployed with CS-Script) either globally or per-script. Read more about it here.

Editing scripts

The scripts can be edited with any suitable text editor. Though if want to take advantage of Intellisense you may prefer Visual Studio with CS-Script extension. The extension can be enabled via VS Extension Manager.
Though in many cases you may be better off with just Notepad++. You will need to enable C# intellisense plugin from the plugin manager. THis plugin was developed specifically to allow VS-like experience when dealing with C# scripts and any C# code. The plugin comes with it's own CS-Script package and allows true C# Intellisence assisted editing as well as the execution and debugging.

Updated Wiki: Standalone Script Execution

$
0
0
Further reading:

Script stand alone execution

Script stand alone execution is the most mature and stable part of CS-Script feature set. Thus the major online documentation has been written quite some time ago and while it may currently have some referencing discrepancies (e.g. VS2012 instead of VS2015) it is still an accurate primary source of CS-Script knowledge. This Wiki article on the other hand is the most concise and concentrated description of the relevant content and in many cases it will be fully sufficient for majority of the readers. In all other cases please refer to the primary Documentation online.

CS-Script allows a direct single step execution of files containing ECMA-compliant C# code.
Canonical Hello World script:
using System;
using System.Windows.Forms;

class Script
{
    staticpublicvoid Main(string[] args)
    {
        Console.WriteLine("Hello World!");
        MessageBox.Show("Hello World!");
    }
}

The script engine executable comes in three forms:
  • cscs.exe - console application executable
  • csws.exe - Windows application executable
  • css.exe - Universal executable. It launches cscs.exe and makes console window visible on the first console output from the script being executed.
The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter.
Thus the minimal deployment file set for CS-Script is either cscs.exe or csws.exe file. Any of these files is sufficient for the wast majority of the scripting scenarios.

Installing CS-Script environment

The simplest way to start using CS-Script is to install it from Chocolatey, which is a repository for the Windows software packages. Chocolatey it is a Windows version of Linux apt-get. CS-Script can be installed with the following command:
C:\> choco install cs-script
During the installation Chocolatey downloads the latest version of CS-Script (from the official releases repository), extracts all files in the default Chocolatey libraries location and executes CS-Script's cssconfig.exe, which configures the OS for running C# scripts. Strictly speaking running cssconfig.exe is optional. The only thing that it does is adding the CS-Script location to the system PATH, and adds Windows Explorer shell extensions (context menu).

Deploying C# scripts

Any OS that have .NET/Mono installed can run C# scripts. The minimal distribution file set is the script engine executable and the script file itself. Of course if the script depends on the third-party library or other scripts then they also need to be deployed on the target system. The easiest way of preparing the script for running on another system is to use Notepad++ with C# Intellisense plugin enabled. Just click the "distro" button and the plugin will analyse the script and package it with all its dependencies along with the script engine itself. Optionally it may convert the script into a self sufficient single executable.

Features

From the start CS-Script was heavily influenced by Python and the developer experiences it delivers. Thus the it tries to match the most useful Python features (apart from the Python syntax). Here are some highlight of the CS-Script features. Some more info can be found here.
  • Scripts are written in 'plain vanilla' CLS-compliant C#. Though classless scripts are also supported.
  • Remarkable execution speed matching performance of compiled managed applications.
  • Including (referencing) dependency scripts from the main script.
  • Referencing external assemblies from the script.
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings').
  • Automatic resolving (downloading and referencing) NuGet packages.
  • Building a self sufficient executable from the script.
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script plugin for Notepad++ brings true Intellisense to the 'peoples editor')

Quick guide of CS-Script directives

CS-Script relies on so called 'script directives' for implementing some features. These directives are the C# comments that do not abstract standard C# compilers and yet provide user defined instructions for the script engine.
Simple WebAPI server classless script:
//css_args /ac;           - interpret as auto-class (classless) script//css_include webapi;     - include webapi.cs script//css_include my_dto.cs;  - include DTO definitions script//css_nuget NLog;         - reference NLog assembly   using System;
using WebApi;

void main()
{
    WebApi.SimpleHost
          .StartAsConosle("http://localhost:8080",
                          server =>
                          {
                              Console.WriteLine("Press Enter to quit.");
                              Console.WriteLine("---------------------");
                              server.Configuration.OutputRouts(Console.WriteLine);
                              Console.WriteLine("---------------------");
                              Console.ReadLine();
                          });
}

Set of supported directives is very small set of commend. The full description with code samples can be found here. The following is a set of the most useful directives:
//css_inc - import/include another C# script (similar to C++ #include).
//css_ref - reference .NET assembly.
//css_args - provide startup arguments.
//css_dir - set additional probing directories for dependency scripts and assemblies.
//css_nuget - reference (and download if not present) NuGet package.
Note, due to the fact that Microsoft stopped distributing the latest C# compiler with it's .NET deployment, support for C# 6.0 syntax requires one extra step to be done. You will need to indicate the location of the Roslyn compiler (deployed with CS-Script) either globally or per-script. Read more about it here.

Editing scripts

The scripts can be edited with any suitable text editor. Though if want to take advantage of Intellisense you may prefer Visual Studio with CS-Script extension. The extension can be enabled via VS Extension Manager.
Though in many cases you may be better off with just Notepad++. You will need to enable C# intellisense plugin from the plugin manager. THis plugin was developed specifically to allow VS-like experience when dealing with C# scripts and any C# code. The plugin comes with it's own CS-Script package and allows true C# Intellisence assisted editing as well as the execution and debugging.

Updated Wiki: Documentation

$
0
0
For online help and other product related information please visitCS-Script Home Page.The help content can also be downloaded form Releases page.
Further reading:

CS-Script Overview

CS-Script is one of the most mature C# scripting frameworks available today. CS-Script first public release is dated back to 2004. At that time .NET ecosystem didn't provide any scripting solution. What is even more intriguing is the fact that at that time Microsoft consistently refused any call for such a solution. It insisted that there is no need for scripting as it has no place in .NET domain. Thus CS-Script was a pioneering effort to bring scripting experience to the developers when .NET vendor failed to do so. The main idea of CS-Script is to allow "plain vanilla" C# code execution from both command-prompt and form any CLR application hosting the script engine.

Things have changed since then. Today Microsoft is behind the ongoing Roslyn effort - their own compiler-as-service solution. Mono also has their compiler-as-service (Mono.CSharp) released around 2010. Though CS-Script remains one of the best choices of scripting platform due to the unorthodox conceptual approaches and unique features not found in other solutions.

What makes CS-Script almost unique is the comprehensive approach to scripting as a software development technique that has various applications (beyond just simple "eval"). CS-Script was hugely influenced by Python. In fact many CS-Scrip features was 'borrowed' from Python: referencing external scripts and assemblies, caching, converting scripts into self-sufficient executables. Thus having Python in mind CS-Script provided the answer for both standalone (script file) execution by the script engine executable and hosted script (file or in-memory text) execution by the script engine hosted by the user application.

For the hosted script execution CS-Script provides one unified interface, which allows switching between underlying compiling services (Mono, Roslyn and CodeDom) without the need to change the hosting code.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
                                //EvaluatorEngine.Mono;//EvaluatorEngine.CodeDom;var sqr = CSScript.Evaluator
                  .CreateDelegate(@"int Sqr(int a)
                                    {
                                        return a * a;
                                    }");

var r = sqr(3);

Script stand alone execution

Additional documentation

The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. The script file is a file containing an ordinary ECMA-compliant C# code. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono. CS-Script is implements script caching that ensures that the script file is never compiler unless it has change since the first execution or it is the first execution. This in turn allows a remarkable performance for the script execution, which is identical to the performance of the fully compiled assembly. Thus CS-Script implements JIT compilation but not within the scope of the code fragments (e.g. .NET JIT compilation) but within the scope of whole script file.

Some of the important features of CS-Script are:
  • Including (referencing) dependency scripts from the main script
  • Referencing external assemblies from the script
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings')
  • Automatic resolving (downloading and referencing) NuGet packages
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script extension for Notepad++ brings true Intellisense to the 'peoples editor')
  • ...

You can find all the details about the stand alone script execution in the online documentation.

The default CS-Script distribution includes various components that make scripting a comfortable experience and yet it in may cases a single script engine file is sufficient for the scripting execution.

The most convenient way of installing CS-Script is to get it from Chocolaty (Windows equivalent of Linux apt-get)

NOTE: Due to the fact that C# 6.0 compiler (Roslyn) is a beta product it is not enabled by default. The full C# 6.0 support overview can be found here: .NET 4.6 Support.

Hosted script execution

Additional documentation

CS-Script can be hosted by any CLR application. The easiest way to do this is to add it as a NuGet package.
CS-Script allows executing entire script files or C# code fragments. The very original implementation of hosted script execution was based on CodeDom API, which is available in all distributions of .NET and Mono. Thus it combines low dependency and ultimate portability.

Later CS-Script support alternative (compiler-as-service) compiling services has been added:
  • End of 2012 CS-Script v3.5: Mono Evaluator (Mono.CSharp.dll v.4.0.0.0)
  • End of 2015 CS-Script v3.10: Roslyn Compilers Beta release v1.2.0-beta-20151211-01

It is entirely up to developer to decide which compiling engine to use. The choice can be dictated by performance, deployment and maintainability considerations but not the convenience of the hosting API. This is because CS-Script encapsulates all three supported C# compilers into a single unified interface CSScript.Evaluator. Thus changing the compiling engine becomes a merely configuration exercise.

The evaluator allow executing either code fragments or entire class(es) definitions. Script can automatically access host types without any restrictions except types visibility (public vs. private). Scripted objects can be accessed dynamically (via 'dynamic') or statically via interface inheritance or duck-typing or via strongly typed delegates:

Accessing the script members dynamically:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
         
int result = script.Sum(1, 2);

Loading strongly typed delegate:

var product = CSScript.Evaluator
                      .LoadDelegate<Func<int, int, int>>(
                                  @"int Product(int a, int b)
                                    {
                                        return a * b;
                                    }");
int result = product(3, 2);

Interface alignment (duck-typing). Note that class Script doesn't inherit from ICalc. Instead the script engine wraps the script object into dynamically generated proxy of ICals type:

publicinterface ICalc
{
    int Sum(int a, int b);
}
...
ICalc script = CSScript.Evaluator
                       .LoadCode<ICalc>(@"using System;
                                          public class Script
                                          {
                                              public int Sum(int a, int b)
                                              {
                                                  return a+b;
                                              }
                                          }");
      
int result = script.Sum(1, 2);

While Mono and Roslyn bring some benefits in many cases CodeDom driven execution remains the most practical solution. Particularly because it is the only solution that allows script debugging.
You can find all the details about the hosted script execution on this page: Hosted Script Execution.

Source code checked in, #36de41fe27314f71b4623f7bb49f617779153e15

$
0
0
Release v3.11.0.0 * Implemented Async CSScript.Evaluator extensions * Implemented Remoting CSScript.Evaluator extensions for unloading compiled script domain. * Added Roslyn server (VBCSCompiler.exe) to keep Roslyn compilers loaded between script executions. Significantly improves Roslyn performance. * Added support //css_nuget package dependency resolving. * Fixed problem with -force[:delay] switch colliding with other //css_nuget switches.

Released: Release v3.11.0.0 (Feb 02, 2016)

$
0
0
  • Implemented Async CSScript.Evaluator extensions for all public Evaluator API
  • Implemented Remoting CSScript.Evaluator extensions for unloading compiled script domain.
  • Added Roslyn server (VBCSCompiler.exe) to keep Roslyn compilers loaded between script executions. Significantly improves Roslyn performance.
  • Added support //css_nuget package dependency resolving.
  • Fixed problem with -force[:delay] switch colliding with other //css_nuget switches

Async extensions
var product = await CSScript.Evaluator
                            .LoadDelegateAsync
                                    <Func<int, int, int>>(
                                   @"int Product(int a, int b)
                                     {
                                         return a * b;
                                     }");

int result = product(4, 2);

Unloading loaded scripts
var sum = CSScript.Evaluator
                  .CreateDelegateRemotely<int>(@"int Sum(int a, int b)
                                                 {
                                                     return a+b;
                                                 }");

int result = sum(15, 3));

sum.UnloadOwnerDomain();

Updated Release: Release v3.11.0.0 (Feb 02, 2016)

$
0
0
  • Implemented Async CSScript.Evaluator extensions for all public Evaluator API
  • Implemented Remoting CSScript.Evaluator extensions for unloading compiled script domain.
  • Added Roslyn server (VBCSCompiler.exe) to keep Roslyn compilers loaded between script executions. Significantly improves Roslyn performance.
  • Added support //css_nuget package dependency resolving.
  • Fixed problem with -force[:delay] switch colliding with other //css_nuget switches

Async extensions
var product = await CSScript.Evaluator
                            .LoadDelegateAsync
                                    <Func<int, int, int>>(
                                   @"int Product(int a, int b)
                                     {
                                         return a * b;
                                     }");

int result = product(4, 2);

Unloading loaded scripts
var sum = CSScript.Evaluator
                  .CreateDelegateRemotely<int>(@"int Sum(int a, int b)
                                                 {
                                                     return a+b;
                                                 }");

int result = sum(15, 3));

sum.UnloadOwnerDomain();

Source code checked in, #a0c1873c276ca78a0b29842c6d15ab9920f52df5

$
0
0
CSScript.Evaluator added processing //css_dir

Source code checked in, #75a6b2bbb8db0476cdab932885e026afe2d88a68

$
0
0
Release v3.11.1.0 - HotFix CSScript.Evaluator: - Added processing probing dirs from CSScript.GlobalSettings - Added automatic assembly referencing from code

Updated Wiki: Documentation

$
0
0
For online help and other product related information please visitCS-Script Home Page.The help content can also be downloaded form Releases page.
Further reading:

CS-Script Overview

CS-Script compared to other scripting solutions

When you start exploring CS-Script you will probably quickly notice that it is quite different comparing to other scripting solutions. The immediate candidates for comparison are Mono and Roslyn. Both of these solutions are very similar. They concentrate mostly on hosting the script engine and executing small pieces of code. Their documentation offers plenty of samples of executing simple statements like '1+2' or 'Console.WriteLine("Hello")'. It all makes an impression that these script engines were designed for a single objective - providing a hosting solution for execution of user specified C# fragments (e.g. REPL or work flow engine). The usual consideration scope of these solutions is often no larger then a single C# statement. Thus they offer no solution for:
  • Executing a complete C# module (C# file).
  • Executing C# file as an external process (e.g. from command-prompt).
  • Linking multiple inter-dependent C# files at runtime and executing them as a single unit.
  • Convenient way of referencing dependency assemblies.
  • Debugging scripts.
  • A 14 years old (since 2002) problem of memory leaking with every single C# statement executed in the current AppDomain.
  • ...
Both Roslyn and Mono are fully capable of being used to build a derived solutions addressing the challenges listed above but they don't deal with them directly.

CS-Script is different as its objective is to provide a platform for executing scripts of any level of complexity and dependency as well as extending the applications with the functionality as rich as .NET can facilitate. In a way CS-Script to C# syntax is what Python platform to Python syntax.

CS-Script does answer the all scripting challenges including REPL and IDE. But what even more important CS-Script provides one unified interface (API) that normalizes all three major C# compiling technologies CodeDom, Roslyn and Mono. Thus while letting developers to have a single scripting codebase CS-Script makes the choice of a concrete compiling technology (e.g. Mono vs Roslyn) a simple matter of the configuration. For the complete comparative overview of the compiling technologies see the dedicated Script Hosting page.

Background

CS-Script is one of the most mature C# scripting frameworks available today. CS-Script first public release is dated back to 2004. At that time .NET ecosystem didn't provide any scripting solution. What is even more intriguing is the fact that at that time Microsoft consistently refused any call for such a solution. It insisted that there is no need for scripting as it has no place in .NET domain. Thus CS-Script was a pioneering effort to bring scripting experience to the developers when .NET vendor failed to do so. The main idea of CS-Script is to allow "plain vanilla" C# code execution from both command-prompt and form any CLR application hosting the script engine.

Things have changed since then. Today Microsoft is behind the ongoing Roslyn effort - their own compiler-as-service solution. Mono also has their compiler-as-service (Mono.CSharp) released around 2010. Though CS-Script remains one of the best choices of scripting platform due to the unorthodox conceptual approaches and unique features not found in other solutions.

What makes CS-Script almost unique is the comprehensive approach to scripting as a software development technique that has various applications (beyond just simple "eval"). CS-Script was hugely influenced by Python. In fact many CS-Scrip features was 'borrowed' from Python: referencing external scripts and assemblies, caching, converting scripts into self-sufficient executables. Thus having Python in mind CS-Script provided the answer for both standalone (script file) execution by the script engine executable and hosted script (file or in-memory text) execution by the script engine hosted by the user application.

For the hosted script execution CS-Script provides one unified interface, which allows switching between underlying compiling services (Mono, Roslyn and CodeDom) without the need to change the hosting code.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
                                //EvaluatorEngine.Mono;//EvaluatorEngine.CodeDom;var sqr = CSScript.Evaluator
                  .CreateDelegate(@"int Sqr(int a)
                                    {
                                        return a * a;
                                    }");

var r = sqr(3);

Script stand alone execution

Additional documentation

The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. The script file is a file containing an ordinary ECMA-compliant C# code. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono. CS-Script is implements script caching that ensures that the script file is never compiler unless it has change since the first execution or it is the first execution. This in turn allows a remarkable performance for the script execution, which is identical to the performance of the fully compiled assembly. Thus CS-Script implements JIT compilation but not within the scope of the code fragments (e.g. .NET JIT compilation) but within the scope of whole script file.

Some of the important features of CS-Script are:
  • Including (referencing) dependency scripts from the main script
  • Referencing external assemblies from the script
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings')
  • Automatic resolving (downloading and referencing) NuGet packages
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script extension for Notepad++ brings true Intellisense to the 'peoples editor')
  • ...

You can find all the details about the stand alone script execution in the online documentation.

The default CS-Script distribution includes various components that make scripting a comfortable experience and yet it in may cases a single script engine file is sufficient for the scripting execution.

The most convenient way of installing CS-Script is to get it from Chocolaty (Windows equivalent of Linux apt-get)

NOTE: Due to the fact that C# 6.0 compiler (Roslyn) is a beta product it is not enabled by default. The full C# 6.0 support overview can be found here: .NET 4.6 Support.

Hosted script execution

Additional documentation

CS-Script can be hosted by any CLR application. The easiest way to do this is to add it as a NuGet package.
CS-Script allows executing entire script files or C# code fragments. The very original implementation of hosted script execution was based on CodeDom API, which is available in all distributions of .NET and Mono. Thus it combines low dependency and ultimate portability.

Later CS-Script support alternative (compiler-as-service) compiling services has been added:
  • End of 2012 CS-Script v3.5: Mono Evaluator (Mono.CSharp.dll v.4.0.0.0)
  • End of 2015 CS-Script v3.10: Roslyn Compilers Beta release v1.2.0-beta-20151211-01

It is entirely up to developer to decide which compiling engine to use. The choice can be dictated by performance, deployment and maintainability considerations but not the convenience of the hosting API. This is because CS-Script encapsulates all three supported C# compilers into a single unified interface CSScript.Evaluator. Thus changing the compiling engine becomes a merely configuration exercise.

The evaluator allow executing either code fragments or entire class(es) definitions. Script can automatically access host types without any restrictions except types visibility (public vs. private). Scripted objects can be accessed dynamically (via 'dynamic') or statically via interface inheritance or duck-typing or via strongly typed delegates:

Accessing the script members dynamically:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
         
int result = script.Sum(1, 2);

Loading strongly typed delegate:

var product = CSScript.Evaluator
                      .LoadDelegate<Func<int, int, int>>(
                                  @"int Product(int a, int b)
                                    {
                                        return a * b;
                                    }");
int result = product(3, 2);

Interface alignment (duck-typing). Note that class Script doesn't inherit from ICalc. Instead the script engine wraps the script object into dynamically generated proxy of ICals type:

publicinterface ICalc
{
    int Sum(int a, int b);
}
...
ICalc script = CSScript.Evaluator
                       .LoadCode<ICalc>(@"using System;
                                          public class Script
                                          {
                                              public int Sum(int a, int b)
                                              {
                                                  return a+b;
                                              }
                                          }");
      
int result = script.Sum(1, 2);

While Mono and Roslyn bring some benefits in many cases CodeDom driven execution remains the most practical solution. Particularly because it is the only solution that allows script debugging.
You can find all the details about the hosted script execution on this page: Hosted Script Execution.

Updated Wiki: Home

$
0
0

CS-Script Source


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

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 CS-Script has been downloaded 239,477 times world wide.

This site hosts the source code, releases and the generic documentation. For the detailed online documentation information please visit CS-Script Home Page.

A few code snippets below can give you the idea what CS-Script is about:

(doesn't demonstrate running standalons scrips but hosted scripts only)

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

 

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

C:\> cscs script.cs

 

 

 
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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791 

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.

 

 

Updated Wiki: Home

$
0
0

CS-Script Source


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

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 CS-Script has been downloaded 239,477 times world wide.

This site hosts the source code, releases and the generic documentation. For the detailed online documentation please visit CS-Script Home Page.

A few code snippets below can give you the idea what CS-Script is about:

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

 

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

C:\> cscs script.cs

 

 

 
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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791 

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.

 

 

Updated Wiki: Home

$
0
0

CS-Script Source


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

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 CS-Script has been downloaded 239,477 times world wide.

This site hosts the source code, releases and the generic documentation.

For the detailed online documentation please visit CS-Script Home Page.

A few code snippets below can give you the idea what CS-Script is about:

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

 

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

C:\> cscs script.cs

 

 

 
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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791 

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.

 

 

Updated Wiki: Home

$
0
0

CS-Script Source

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

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 CS-Script has been downloaded239,477 times world wide.

At this site you will find:

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

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

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

 

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

C:\> cscs script.cs

 

 

 
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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791 

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.

 

 

Updated Wiki: Home

$
0
0

CS-Script Source

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: MediaPortalFlashDevelopK2 APISF.net ("WinTin"), BonSAIAyaNova (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 CS-Script has been downloaded239,477 times world wide.

At this site you will find:

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

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

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

 

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

C:\> cscs script.cs

 

 

 
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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791 

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.

 

 

Updated Wiki: images


Updated Wiki: images

Updated Wiki: Documentation

$
0
0
For online help and other product related information please visitCS-Script Home Page.The help content can also be downloaded form Releases page.
Further reading:

CS-Script Overview

CS-Script compared to other scripting solutions

When you start exploring CS-Script you will probably quickly notice that it is quite different comparing to other scripting solutions. The immediate candidates for comparison are Mono and Roslyn. Both of these solutions are very similar. They concentrate mostly on hosting the script engine and executing small pieces of code. Their documentation offers plenty of samples of executing simple statements like '1+2' or 'Console.WriteLine("Hello")'. It all makes an impression that these script engines were designed for a single objective - providing a hosting solution for execution of user specified C# fragments (e.g. REPL or work flow engine). The usual consideration scope of these solutions is often no larger then a single C# statement. Thus they offer no solution for:
  • Executing a complete C# module (C# file).
  • Executing C# file as an external process (e.g. from command-prompt).
  • Linking multiple inter-dependent C# files at runtime and executing them as a single unit.
  • Convenient way of referencing dependency assemblies.
  • Debugging scripts.
  • A 14 years old (since 2002) problem of memory leaking with every single C# statement executed in the current AppDomain.
  • ...
Both Roslyn and Mono are fully capable of being used to build a derived solutions addressing the challenges listed above but they don't deal with them directly.

CS-Script is different as its objective is to provide a platform for executing scripts of any level of complexity and dependency as well as extending the applications with the functionality as rich as .NET can facilitate. In a way CS-Script to C# syntax is what Python platform to Python syntax.

CS-Script does answer the all scripting challenges including REPL and IDE. But what even more important CS-Script provides one unified interface (API) that normalizes all three major C# compiling technologies CodeDom, Roslyn and Mono. Thus while letting developers to have a single scripting codebase CS-Script makes the choice of a concrete compiling technology (e.g. Mono vs Roslyn) a simple matter of the configuration. For the complete comparative overview of the compiling technologies see the dedicated Script Hosting page.

Background

CS-Script is one of the most mature C# scripting frameworks available today. CS-Script first public release is dated back to 2004. At that time .NET ecosystem didn't provide any scripting solution. What is even more intriguing is the fact that at that time Microsoft consistently refused any call for such a solution. It insisted that there is no need for scripting as it has no place in .NET domain. Thus CS-Script was a pioneering effort to bring scripting experience to the developers when .NET vendor failed to do so. The main idea of CS-Script is to allow "plain vanilla" C# code execution from both command-prompt and form any CLR application hosting the script engine.

Things have changed since then. Today Microsoft is behind the ongoing Roslyn effort - their own compiler-as-service solution. Mono also has their compiler-as-service (Mono.CSharp) released around 2010. Though CS-Script remains one of the best choices of scripting platform due to the unorthodox conceptual approaches and unique features not found in other solutions.

What makes CS-Script almost unique is the comprehensive approach to scripting as a software development technique that has various applications (beyond just simple "eval"). CS-Script was hugely influenced by Python. In fact many CS-Scrip features was 'borrowed' from Python: referencing external scripts and assemblies, caching, converting scripts into self-sufficient executables. Thus having Python in mind CS-Script provided the answer for both standalone (script file) execution by the script engine executable and hosted script (file or in-memory text) execution by the script engine hosted by the user application.

For the hosted script execution CS-Script provides one unified interface, which allows switching between underlying compiling services (Mono, Roslyn and CodeDom) without the need to change the hosting code.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
                                //EvaluatorEngine.Mono;//EvaluatorEngine.CodeDom;var sqr = CSScript.Evaluator
                  .CreateDelegate(@"int Sqr(int a)
                                    {
                                        return a * a;
                                    }");

var r = sqr(3);

Script stand alone execution

Additional documentation

The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. The script file is a file containing an ordinary ECMA-compliant C# code. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono. CS-Script is implements script caching that ensures that the script file is never compiler unless it has change since the first execution or it is the first execution. This in turn allows a remarkable performance for the script execution, which is identical to the performance of the fully compiled assembly. Thus CS-Script implements JIT compilation but not within the scope of the code fragments (e.g. .NET JIT compilation) but within the scope of whole script file.

Some of the important features of CS-Script are:
  • Including (referencing) dependency scripts from the main script
  • Referencing external assemblies from the script
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings')
  • Automatic resolving (downloading and referencing) NuGet packages
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script extension for Notepad++ brings true Intellisense to the 'peoples editor')
  • ...

You can find all the details about the stand alone script execution in the online documentation.

The default CS-Script distribution includes various components that make scripting a comfortable experience and yet it in may cases a single script engine file is sufficient for the scripting execution.

The most convenient way of installing CS-Script is to get it from Chocolaty (Windows equivalent of Linux apt-get)

NOTE: Due to the fact that C# 6.0 compiler (Roslyn) is a beta product it is not enabled by default. The full C# 6.0 support overview can be found here: .NET 4.6 Support.

Hosted script execution

Additional documentation

CS-Script can be hosted by any CLR application. The easiest way to do this is to add it as a NuGet package.
CS-Script allows executing entire script files or C# code fragments. The very original implementation of hosted script execution was based on CodeDom API, which is available in all distributions of .NET and Mono. Thus it combines low dependency and ultimate portability.

Later CS-Script support alternative (compiler-as-service) compiling services has been added:
  • End of 2012 CS-Script v3.5: Mono Evaluator (Mono.CSharp.dll v.4.0.0.0)
  • End of 2015 CS-Script v3.10: Roslyn Compilers Beta release v1.2.0-beta-20151211-01

It is entirely up to developer to decide which compiling engine to use. The choice can be dictated by performance, deployment and maintainability considerations but not the convenience of the hosting API. This is because CS-Script encapsulates all three supported C# compilers into a single unified interface CSScript.Evaluator. Thus changing the compiling engine becomes a merely configuration exercise.

The evaluator allow executing either code fragments or entire class(es) definitions. Script can automatically access host types without any restrictions except types visibility (public vs. private). Scripted objects can be accessed dynamically (via 'dynamic') or statically via interface inheritance or duck-typing or via strongly typed delegates:

Accessing the script members dynamically:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
         
int result = script.Sum(1, 2);

Loading strongly typed delegate:

var product = CSScript.Evaluator
                      .LoadDelegate<Func<int, int, int>>(
                                  @"int Product(int a, int b)
                                    {
                                        return a * b;
                                    }");
int result = product(3, 2);

Interface alignment (duck-typing). Note that class Script doesn't inherit from ICalc. Instead the script engine wraps the script object into dynamically generated proxy of ICals type:

publicinterface ICalc
{
    int Sum(int a, int b);
}
...
ICalc script = CSScript.Evaluator
                       .LoadCode<ICalc>(@"using System;
                                          public class Script
                                          {
                                              public int Sum(int a, int b)
                                              {
                                                  return a+b;
                                              }
                                          }");
      
int result = script.Sum(1, 2);

While Mono and Roslyn bring some benefits in many cases CodeDom driven execution remains the most practical solution. Particularly because it is the only solution that allows script debugging.
You can find all the details about the hosted script execution on this page: Hosted Script Execution.

OS integration, IDE and REPL

Interactive environments are essential part of any software development platform. Majority of scripting scripting solutions come with a simple REPL included. While more complex richer IDEs are usually developed by third-parties.
  • A simple third-party REPL is included into CS-Script ExtensionPack (cs-script/lib/tools/CSI). However it is rather not as comprehensive as similar products available from other vendors (e.g. Mono).
Instead of developing its own REPL CS-Script makes a strong emphasis on a dedicated rich IDE. The choice of IDE instead or REPL is a recognition of the fact that the complexity of the production level scripting is by far higher then the use-case of REPL (rather 'educational' ny nature) and an industry standard IDE is a more appropriate choice.

When developing a stand alone scripts you have two IDEs to choose from:

Visual Studio
CS-Script integration with Visual studio comes in two forms.

Shell extensions
If you have CS-Script installed (e.g. with Chocolatey) you can just open any script file in visual Studio with CS-Script specific shell context menu:

The script file will not be just loaded in VS but the appropriate project infrastructure will be created on the fly. Thus you will be able to take advantage of the full power of VS and edit, execute and debug your script as any other C# code.
Visual Studio 'CS-Script Tools' extension
This extension deliveres the same experience as provided by shell extensions but without leaving Visual Sudio.
See Visual Studio Gallery page for more details.

Notepad
While arguably nothing can compete with the power of Visual Studio in many cases the 'peoples editor' Notepad++ is a more attractive and practical alternative.

CS-Script products family include a powerful 'CS-Script (Intellisense)' Notepad++ plugin. While editing (with true Intellisence), executing and even debugging the scripts you can also benefit from lightning fast nature and rich functionality of Notepad++.

The plugin also offers an interesting deployment opportunities. It includes a copy of the CS-Script engine thus the only thing you need to work with CS-Script C# scripts on any Windows with .NET (v4.0) installed is a copy of Notepad+ with this plugin enabled. The plugin also allows you to package yours scripts so they can be brought and executed on other systems without the need for CS-Script to be installed.

The plugin home page contains plenty details about the product but to put it simple "the plugin brings true VS experiences in Notepad".


Updated Wiki: Documentation

$
0
0
For online help and other product related information please visitCS-Script Home Page.The help content can also be downloaded form Releases page.
Further reading:

CS-Script Overview

CS-Script compared to other scripting solutions

When you start exploring CS-Script you will probably quickly notice that it is quite different comparing to other scripting solutions. The immediate candidates for comparison are Mono and Roslyn. Both of these solutions are very similar. They concentrate mostly on hosting the script engine and executing small pieces of code. Their documentation offers plenty of samples of executing simple statements like '1+2' or 'Console.WriteLine("Hello")'. It all makes an impression that these script engines were designed for a single objective - providing a hosting solution for execution of user specified C# fragments (e.g. REPL or work flow engine). The usual consideration scope of these solutions is often no larger then a single C# statement. Thus they offer no solution for:
  • Executing a complete C# module (C# file).
  • Executing C# file as an external process (e.g. from command-prompt).
  • Linking multiple inter-dependent C# files at runtime and executing them as a single unit.
  • Convenient way of referencing dependency assemblies.
  • Debugging scripts.
  • A 14 years old (since 2002) problem of memory leaking with every single C# statement executed in the current AppDomain.
  • ...
Both Roslyn and Mono are fully capable of being used to build a derived solutions addressing the challenges listed above but they don't deal with them directly.

CS-Script is different as its objective is to provide a platform for executing scripts of any level of complexity and dependency as well as extending the applications with the functionality as rich as .NET can facilitate. In a way CS-Script to C# syntax is what Python platform to Python syntax.

CS-Script does answer the all scripting challenges including REPL and IDE. But what even more important CS-Script provides one unified interface (API) that normalizes all three major C# compiling technologies CodeDom, Roslyn and Mono. Thus while letting developers to have a single scripting codebase CS-Script makes the choice of a concrete compiling technology (e.g. Mono vs Roslyn) a simple matter of the configuration. For the complete comparative overview of the compiling technologies see the dedicated Script Hosting page.

Background

CS-Script is one of the most mature C# scripting frameworks available today. CS-Script first public release is dated back to 2004. At that time .NET ecosystem didn't provide any scripting solution. What is even more intriguing is the fact that at that time Microsoft consistently refused any call for such a solution. It insisted that there is no need for scripting as it has no place in .NET domain. Thus CS-Script was a pioneering effort to bring scripting experience to the developers when .NET vendor failed to do so. The main idea of CS-Script is to allow "plain vanilla" C# code execution from both command-prompt and form any CLR application hosting the script engine.

Things have changed since then. Today Microsoft is behind the ongoing Roslyn effort - their own compiler-as-service solution. Mono also has their compiler-as-service (Mono.CSharp) released around 2010. Though CS-Script remains one of the best choices of scripting platform due to the unorthodox conceptual approaches and unique features not found in other solutions.

What makes CS-Script almost unique is the comprehensive approach to scripting as a software development technique that has various applications (beyond just simple "eval"). CS-Script was hugely influenced by Python. In fact many CS-Scrip features was 'borrowed' from Python: referencing external scripts and assemblies, caching, converting scripts into self-sufficient executables. Thus having Python in mind CS-Script provided the answer for both standalone (script file) execution by the script engine executable and hosted script (file or in-memory text) execution by the script engine hosted by the user application.

For the hosted script execution CS-Script provides one unified interface, which allows switching between underlying compiling services (Mono, Roslyn and CodeDom) without the need to change the hosting code.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
                                //EvaluatorEngine.Mono;//EvaluatorEngine.CodeDom;var sqr = CSScript.Evaluator
                  .CreateDelegate(@"int Sqr(int a)
                                    {
                                        return a * a;
                                    }");

var r = sqr(3);

Script stand alone execution

Additional documentation

The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. The script file is a file containing an ordinary ECMA-compliant C# code. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono. CS-Script is implements script caching that ensures that the script file is never compiler unless it has change since the first execution or it is the first execution. This in turn allows a remarkable performance for the script execution, which is identical to the performance of the fully compiled assembly. Thus CS-Script implements JIT compilation but not within the scope of the code fragments (e.g. .NET JIT compilation) but within the scope of whole script file.

Some of the important features of CS-Script are:
  • Including (referencing) dependency scripts from the main script
  • Referencing external assemblies from the script
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings')
  • Automatic resolving (downloading and referencing) NuGet packages
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script extension for Notepad++ brings true Intellisense to the 'peoples editor')
  • ...

You can find all the details about the stand alone script execution in the online documentation.

The default CS-Script distribution includes various components that make scripting a comfortable experience and yet it in may cases a single script engine file is sufficient for the scripting execution.

The most convenient way of installing CS-Script is to get it from Chocolaty (Windows equivalent of Linux apt-get)

NOTE: Due to the fact that C# 6.0 compiler (Roslyn) is a beta product it is not enabled by default. The full C# 6.0 support overview can be found here: .NET 4.6 Support.

Hosted script execution

Additional documentation

CS-Script can be hosted by any CLR application. The easiest way to do this is to add it as a NuGet package.
CS-Script allows executing entire script files or C# code fragments. The very original implementation of hosted script execution was based on CodeDom API, which is available in all distributions of .NET and Mono. Thus it combines low dependency and ultimate portability.

Later CS-Script support alternative (compiler-as-service) compiling services has been added:
  • End of 2012 CS-Script v3.5: Mono Evaluator (Mono.CSharp.dll v.4.0.0.0)
  • End of 2015 CS-Script v3.10: Roslyn Compilers Beta release v1.2.0-beta-20151211-01

It is entirely up to developer to decide which compiling engine to use. The choice can be dictated by performance, deployment and maintainability considerations but not the convenience of the hosting API. This is because CS-Script encapsulates all three supported C# compilers into a single unified interface CSScript.Evaluator. Thus changing the compiling engine becomes a merely configuration exercise.

The evaluator allow executing either code fragments or entire class(es) definitions. Script can automatically access host types without any restrictions except types visibility (public vs. private). Scripted objects can be accessed dynamically (via 'dynamic') or statically via interface inheritance or duck-typing or via strongly typed delegates:

Accessing the script members dynamically:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
         
int result = script.Sum(1, 2);

Loading strongly typed delegate:

var product = CSScript.Evaluator
                      .LoadDelegate<Func<int, int, int>>(
                                  @"int Product(int a, int b)
                                    {
                                        return a * b;
                                    }");
int result = product(3, 2);

Interface alignment (duck-typing). Note that class Script doesn't inherit from ICalc. Instead the script engine wraps the script object into dynamically generated proxy of ICals type:

publicinterface ICalc
{
    int Sum(int a, int b);
}
...
ICalc script = CSScript.Evaluator
                       .LoadCode<ICalc>(@"using System;
                                          public class Script
                                          {
                                              public int Sum(int a, int b)
                                              {
                                                  return a+b;
                                              }
                                          }");
      
int result = script.Sum(1, 2);

While Mono and Roslyn bring some benefits in many cases CodeDom driven execution remains the most practical solution. Particularly because it is the only solution that allows script debugging.
You can find all the details about the hosted script execution on this page: Hosted Script Execution.

OS integration, IDE and REPL

Interactive environments are essential part of any software development platform. Majority of scripting scripting solutions come with a simple REPL included. While more complex richer IDEs are usually developed by third-parties.

A simple third-party REPL is included into CS-Script ExtensionPack (cs-script/lib/tools/CSI). However it is rather not as comprehensive as similar products available from other vendors (e.g. Mono).
Instead of developing its own REPL CS-Script makes a strong emphasis on a dedicated rich IDE. The choice of IDE instead or REPL is a recognition of the fact that the complexity of the production level scripting is by far higher then the use-case of REPL (rather 'educational' ny nature) and an industry standard IDE is a more appropriate choice.

When developing a stand alone scripts you have two IDEs to choose from:

Visual Studio
CS-Script integration with Visual studio comes in two forms.

Shell extensions
If you have CS-Script installed (e.g. with Chocolatey) you can just open any script file in visual Studio with CS-Script specific shell context menu:

The script file will not be just loaded in VS but the appropriate project infrastructure will be created on the fly. Thus you will be able to take advantage of the full power of VS and edit, execute and debug your script as any other C# code.
Visual Studio 'CS-Script Tools' extension
This extension deliveres the same experience as provided by shell extensions but without leaving Visual Sudio.
See Visual Studio Gallery page for more details.

Notepad++
While arguably nothing can compete with the power of Visual Studio in many cases the 'peoples editor' Notepad++ is a more attractive and practical alternative.

CS-Script products family include a powerful 'CS-Script (Intellisense)' Notepad++ plugin. While editing (with true Intellisence), executing and even debugging the scripts you can also benefit from lightning fast nature and rich functionality of Notepad++.

The plugin also offers an interesting deployment opportunities. It includes a copy of the CS-Script engine thus the only thing you need to work with CS-Script C# scripts on any Windows with .NET (v4.0+) installed is a copy of Notepad++ with this plugin enabled. The plugin also allows you to package yours scripts so they can be brought and executed on other systems without the need for CS-Script to be installed.

The plugin home page contains plenty details about the product but to put it simple "the plugin brings true VS experiences in Notepad".

Updated Wiki: Documentation

$
0
0
For online help and other product related information please visitCS-Script Home Page.The help content can also be downloaded form Releases page.
Further reading:

CS-Script Overview

CS-Script compared to other scripting solutions

When you start exploring CS-Script you will probably quickly notice that it is quite different comparing to other scripting solutions. The immediate candidates for comparison are Mono and Roslyn. Both of these solutions are very similar. They concentrate mostly on hosting the script engine and executing small pieces of code. Their documentation offers plenty of samples of executing simple statements like '1+2' or 'Console.WriteLine("Hello")'. It all makes an impression that these script engines were designed for a single objective - providing a hosting solution for execution of user specified C# fragments (e.g. REPL or work flow engine). The usual consideration scope of these solutions is often no larger then a single C# statement. Thus they offer no solution for:
  • Executing a complete C# module (C# file).
  • Executing C# file as an external process (e.g. from command-prompt).
  • Linking multiple inter-dependent C# files at runtime and executing them as a single unit.
  • Convenient way of referencing dependency assemblies.
  • Debugging scripts.
  • A 14 years old (since 2002) problem of memory leaking with every single C# statement executed in the current AppDomain.
  • ...
Both Roslyn and Mono are fully capable of being used to build a derived solutions addressing the challenges listed above but they don't deal with them directly.

CS-Script is different as its objective is to provide a platform for executing scripts of any level of complexity and dependency as well as extending the applications with the functionality as rich as .NET can facilitate. In a way CS-Script to C# syntax is what Python platform to Python syntax.

CS-Script does answer the all scripting challenges including REPL and IDE. But what even more important CS-Script provides one unified interface (API) that normalizes all three major C# compiling technologies CodeDom, Roslyn and Mono. Thus while letting developers to have a single scripting codebase CS-Script makes the choice of a concrete compiling technology (e.g. Mono vs Roslyn) a simple matter of the configuration. For the complete comparative overview of the compiling technologies see the dedicated Script Hosting page.

Background

CS-Script is one of the most mature C# scripting frameworks available today. CS-Script first public release is dated back to 2004. At that time .NET ecosystem didn't provide any scripting solution. What is even more intriguing is the fact that at that time Microsoft consistently refused any call for such a solution. It insisted that there is no need for scripting as it has no place in .NET domain. Thus CS-Script was a pioneering effort to bring scripting experience to the developers when .NET vendor failed to do so. The main idea of CS-Script is to allow "plain vanilla" C# code execution from both command-prompt and form any CLR application hosting the script engine.

Things have changed since then. Today Microsoft is behind the ongoing Roslyn effort - their own compiler-as-service solution. Mono also has their compiler-as-service (Mono.CSharp) released around 2010. Though CS-Script remains one of the best choices of scripting platform due to the unorthodox conceptual approaches and unique features not found in other solutions.

What makes CS-Script almost unique is the comprehensive approach to scripting as a software development technique that has various applications (beyond just simple "eval"). CS-Script was hugely influenced by Python. In fact many CS-Scrip features was 'borrowed' from Python: referencing external scripts and assemblies, caching, converting scripts into self-sufficient executables. Thus having Python in mind CS-Script provided the answer for both standalone (script file) execution by the script engine executable and hosted script (file or in-memory text) execution by the script engine hosted by the user application.

For the hosted script execution CS-Script provides one unified interface, which allows switching between underlying compiling services (Mono, Roslyn and CodeDom) without the need to change the hosting code.

CSScript.EvaluatorConfig.Engine = EvaluatorEngine.Roslyn;
                                //EvaluatorEngine.Mono;//EvaluatorEngine.CodeDom;var sqr = CSScript.Evaluator
                  .CreateDelegate(@"int Sqr(int a)
                                    {
                                        return a * a;
                                    }");

var r = sqr(3);

Script stand alone execution

Additional documentation

The simplest way to execute the C# script is to run the script engine executable (css.exe) and pass the script file as a parameter. The script file is a file containing an ordinary ECMA-compliant C# code. CS-Script currently targets Microsoft implementation of CLR (.NET 2.0/3.0/3.5/4.0/4.5) with full support on Mono. CS-Script is implements script caching that ensures that the script file is never compiler unless it has change since the first execution or it is the first execution. This in turn allows a remarkable performance for the script execution, which is identical to the performance of the fully compiled assembly. Thus CS-Script implements JIT compilation but not within the scope of the code fragments (e.g. .NET JIT compilation) but within the scope of whole script file.

Some of the important features of CS-Script are:
  • Including (referencing) dependency scripts from the main script
  • Referencing external assemblies from the script
  • Automatic referencing external assemblies based on analyses of the script imported namaspaces ('usings')
  • Automatic resolving (downloading and referencing) NuGet packages
  • Possibility to plug in external compiling services for supporting alternative script syntax (e.g. VB, C++)
  • Scripts can be executed on Windows, Linux and OS X (last two will require Mono)
  • Full integration with Windows, Visual Studio and Notepad++ (CS-Script extension for Notepad++ brings true Intellisense to the 'peoples editor')
  • ...

You can find all the details about the stand alone script execution in the online documentation.

The default CS-Script distribution includes various components that make scripting a comfortable experience and yet it in may cases a single script engine file is sufficient for the scripting execution.

The most convenient way of installing CS-Script is to get it from Chocolaty (Windows equivalent of Linux apt-get)

NOTE: Due to the fact that C# 6.0 compiler (Roslyn) is a beta product it is not enabled by default. The full C# 6.0 support overview can be found here: .NET 4.6 Support.

Hosted script execution

Additional documentation

CS-Script can be hosted by any CLR application. The easiest way to do this is to add it as a NuGet package.
CS-Script allows executing entire script files or C# code fragments. The very original implementation of hosted script execution was based on CodeDom API, which is available in all distributions of .NET and Mono. Thus it combines low dependency and ultimate portability.

Later CS-Script support alternative (compiler-as-service) compiling services has been added:
  • End of 2012 CS-Script v3.5: Mono Evaluator (Mono.CSharp.dll v.4.0.0.0)
  • End of 2015 CS-Script v3.10: Roslyn Compilers Beta release v1.2.0-beta-20151211-01

It is entirely up to developer to decide which compiling engine to use. The choice can be dictated by performance, deployment and maintainability considerations but not the convenience of the hosting API. This is because CS-Script encapsulates all three supported C# compilers into a single unified interface CSScript.Evaluator. Thus changing the compiling engine becomes a merely configuration exercise.

The evaluator allow executing either code fragments or entire class(es) definitions. Script can automatically access host types without any restrictions except types visibility (public vs. private). Scripted objects can be accessed dynamically (via 'dynamic') or statically via interface inheritance or duck-typing or via strongly typed delegates:

Accessing the script members dynamically:

dynamic script = CSScript.Evaluator
                         .LoadCode(@"using System;
                                     public class Script
                                     {
                                         public int Sum(int a, int b)
                                         {
                                             return a+b;
                                         }
                                     }");
         
int result = script.Sum(1, 2);

Loading strongly typed delegate:

var product = CSScript.Evaluator
                      .LoadDelegate<Func<int, int, int>>(
                                  @"int Product(int a, int b)
                                    {
                                        return a * b;
                                    }");
int result = product(3, 2);

Interface alignment (duck-typing). Note that class Script doesn't inherit from ICalc. Instead the script engine wraps the script object into dynamically generated proxy of ICals type:

publicinterface ICalc
{
    int Sum(int a, int b);
}
...
ICalc script = CSScript.Evaluator
                       .LoadCode<ICalc>(@"using System;
                                          public class Script
                                          {
                                              public int Sum(int a, int b)
                                              {
                                                  return a+b;
                                              }
                                          }");
      
int result = script.Sum(1, 2);

While Mono and Roslyn bring some benefits in many cases CodeDom driven execution remains the most practical solution. Particularly because it is the only solution that allows script debugging.
You can find all the details about the hosted script execution on this page: Hosted Script Execution.

OS integration, IDE and REPL

Interactive environments are essential part of any software development platform. Majority of scripting scripting solutions come with a simple REPL included. While more complex richer IDEs are usually developed by third-parties.

A simple third-party REPL is included into CS-Script ExtensionPack (cs-script/lib/tools/CSI). However it is rather not as comprehensive as similar products available from other vendors (e.g. Mono).
Instead of developing its own REPL CS-Script makes a strong emphasis on a dedicated rich IDE. The choice of IDE instead or REPL is a recognition of the fact that the complexity of the production level scripting is by far higher then the use-case of REPL (rather 'educational' ny nature) and an industry standard IDE is a more appropriate choice.

When developing a stand alone scripts you have two IDEs to choose from:

Visual Studio
CS-Script integration with Visual studio comes in two forms.

Shell extensions
If you have CS-Script installed (e.g. with Chocolatey) you can just open any script file in visual Studio with CS-Script specific shell context menu:

The script file will not be just loaded in VS but the appropriate project infrastructure will be created on the fly. Thus you will be able to take advantage of the full power of VS and edit, execute and debug your script as any other C# code.
Visual Studio 'CS-Script Tools' extension
This extension deliveres the same experience as provided by shell extensions but without leaving Visual Sudio.
See Visual Studio Gallery page for more details.

Notepad++
While arguably nothing can compete with the power of Visual Studio in many cases the 'peoples editor' Notepad++ is a more attractive and practical alternative.

CS-Script products family include a powerful 'CS-Script (Intellisense)' Notepad++ plugin. While editing (with true Intellisence), executing and even debugging the scripts you can also benefit from lightning fast nature and rich functionality of Notepad++.

The plugin also offers an interesting deployment opportunities. It includes a copy of the CS-Script engine thus the only thing you need to work with CS-Script C# scripts on any Windows with .NET (v4.0+) installed is a copy of Notepad++ with this plugin enabled. The plugin also allows you to package yours scripts so they can be brought and executed on other systems without the need for CS-Script to be installed.

The plugin home page contains plenty details about the product but to put it simple "the plugin brings true VS experiences in Notepad".

Notepad++ project support:


Notepad++ Intellisense:

Updated Wiki: Home

$
0
0

CS-Script Source

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 CS-Script has been downloaded 239,477 times world wide.

At this site you will find:
For the in-depth online documentation please visit CS-Script Home Page.

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

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


CS-Script can execute script files without an additional hosting solution. Just from the command prompt:
C:\> cscs script.cs

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 <root>\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: http://csscriptsource.codeplex.com/releases/view/616791

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.



Viewing all 355 articles
Browse latest View live


Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>