CS-Script uses C# as a scripting language. Therefore, the scripting capabilities are largely based on the language itself. These capabilities can be extended by using external .NET assemblies (FCL or/and third-party assemblies).
It is possible to instruct the script engine to load at execution time any assembly which is referenced within the code. This can be accomplished by any of the following approaches:
Implicit: by placing "using" directive (native C# statement) for namespace directly in the code.
Explicit: by either placing "//css_reference" directive (CS-Script statement) as an explicit user request for loading the assembly or/and using command-line parameter.
The assembly is loaded automatically as a result of the analysis of C# code by the script engine.
The concept of loading the referenced assemblies automatically is quite simple. The script engine analyses all namespaces referenced in the code ("using" statement) and searches "local directories" (see File Location section below) and Global Assembly Cache (GAC) for assemblies with the same name as the referenced namespace. If such assembly is found it is loaded at execution time.
The approach is based on the fact that in a real life there is a strong correlation between the assembly name and the root namespace. For example, the namespace System.Windows.Forms is implemented in the assembly System.Windows.Forms, and the namespace System.XML is implemented in the assembly System.XML . This is also applicable to the assembly file name, which is usually a DLL file with the same name as the assembly name.
Thus, if script code has a statement "using System.Windows.Forms;" the system.windows.forms.dll will be automatically loaded (resolved) form GAC at execution time. If you need to make a use of myLibrary.dll in your script and it has a namespace myLibrary just put "using myLibrary;" in your code and assembly will be resolved.
In any case explicit assembly loading has to be used is where there is no resemblance between a namespace in the assembly and a name of the assembly file (eg. the namespace Steema is implemented in teechart.lite.dll).
The assembly is loaded because of an explicit request either from the code or from the command-line.
Information on loading the assembly from the command-line can be found in the Command-line interface section.
This is the code directive to load the assembly at execution time:
file - name of the assembly file. Assembly names may be enclosed in double quotation marks. Non-searchable assembly names should be enclosed in double quotation marks (see explanations below). Try to avoid using the absolute path because it can create difficulties when isolating scripts for deployment.
The shorter alias directive //css_ref can be used in place of the //css_reference.
The assembly to be loaded must be from one of the following locations (the order indicates the assembly search priority):
the absolute or relative path to assembly directory
Default Script Library directory Script Library (%CSSCRIPT_DIR%\Lib)
Custom Script Library directory(s) (specified in the configuration console SearchDirs)
The assembly math.dll contains
implementation of class Calculator
(which belongs to the namespace Math).
This class has the method
Add(int
a, int b).
The following code explicitly loads two assemblies: math.dll
and system.windows.forms.dll.
//css_reference math.dll; using System; using System.Windows.Forms; class Test { static public void Main( string [] args) { MessageBox.Show(Math.Calculator.Add(1,2).ToString()); } } |
//css_include linq.includes.cs; using System; class Test { static public void Main( string [] args) { .... |
This is the content of the linq.includes.cs file:
//css_ref System.Core; //css_ref System.Data.ComponentModel; //css_ref System.Data.DataSetExtensions; //css_ref System.Xml; //css_ref System.Xml.Linq; |
Command-line interface | CS-Script settings | Using COM (Tutorial)