CS-Script 3.27.0


Using Classless Scripts


Background
Some CS-Script users asked about execution of the classless scripts.

...I know many other scripting languages (JS, python, etc) don't require class and function declarations, yet still allow them if so desired.  Could your engine do the same?...

There is a big difference between Python/JS and CS-Script as development systems. CS-Script is entirely based on the existing language (ECMA C#). In a way CS-Script is an extension of the C# and as such it follows all C# and CLR patterns and constraints. This approach offers a lot of benefits:
Of course it is possible for the script engine to execute classless (free-style) C# code. Even very simple text manipulation with the classless C# code (inserting the code in the empty class declaration) allows to create a standard C# code and execute it. However it would ultimately require creating and maintaining specially designed IDE and/or code editors, special compiler message translators etc. With classless scripts no third-party tools can be used to assists with development. In other words, all benefits listed above would be diminished.

Having said that, it is impossible to ignore the fact that in some situations it would be desirable to have very light scripts with working code only and no code infrastructure. Fortunately, CS-Script offers automatic generation of the wrapper class if the script does not have any class definition. Classless support comes in a few different flavors, which should not be confused:

Execution of classless script files with command line switch /autoclass.
This feature is implemented in the CS-Script engine itself. It is to be used with stand alone scripts:
 - User cannot control the way auto-class is generated.
 - Script must have entry point Main().

Example:
using System;

void Main()
{
    Console.WriteLine(greeting);
}

It is implemented as an internal precompiler thus it can be easily improved by developing your custom "autoclass precompiler" instead. See Precompilers chapter for details.
The details can be found here.

Execution of classless C# code in script hosting scenarios.
This feature is implemented in the CS-Script engine class library (CSScriptLibrary.dll). It is to be used with C# code dynamically executed from the host application:
 - User cannot control the way auto-class is generated.
 - Script is a C# code in memory (not a file).

 Example:
var code = @"public static void Hello(string greeting)
              {
                  Console.WriteLine(greeting);
              }"
;

var SayHello = 
new AsmHelper(CSScript.LoadMethod(code))
                            .
GetStaticMethod();

SayHello("Hello World!");

The details can be found 
here.

Execution of classless script files with Alternative Compiler.
This feature is implemented in the CS-Script engine extension library (CSSCodeProvider.dll). It is to be used with both hosted and standalone scripts:
 - Scripts must have .ccs extension to be handled as classless.
 - User can control the way auto-class is generated (namespace and class name) by using //css_classless directive.

Hosting scenario example  - Client (script.ccs)
//css_classless MyNamespace.MyClass;
using System;

static public void SayHello(string greeting)
{
    Console.WriteLine(greeting);
}


Hosting scenario example  - Host
...
CSScript.GlobalSettings.UseAlternativeCompiler =
            Environment.ExpandEnvironmentVariables(@"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll");

var helper = new AsmHelper(CSScript.Load("script.ccs"));
helper.Invoke("MyNamespace.MyClass.SayHello""Hello World!");

...

The details can be found here.

For the wast majority of scripting scenarios the built-in support for classless scripting is sufficient. If you need more control on auto-class being generated you can use CSSCodeProvider.dll distributed along with the CS-Script binaries.

However if you want ultimate flexibility you can implement Alternative Compiler, which can be considered as a "precompiler" doing some source code manipulations/injections prior the actual compilation with the normal C# compiler. You can find the tutorial for implementing Alternative Compilers here.

Standard CS-Script installation contains examples of classless scripts in the <cs-script>\Samples\Classless directory.

See Also


Autoclass| Script hosting guideline | Alternative Compilers