CS-Script 3.28.2


Importing scripts

Many of scripting tasks can be implemented within a singe file containing C# code. Such approach is the most stable and simple with respect to code maintenances and distribution. However under some circumstances having the whole code in a single file may not be very practical (even for the sake of simplicity of a single file deployment). Whatever development reasons are, CS-Script allows execution of the multiple script files.

This means that if the script A needs to use some code routine that is already implemented in the script B it can do this by importing the script B at execution time. In fact the script B does not have to be a script. It can be any valid C# code. The instruction for the script engine to import some script has to be placed directly in the code of the master script. This way script importing can be nested (script A imports script B which imports script C... ).

This is the directive to import a script:

 //css_import <file>[, preserve_main][, rename_namespace(<oldName>, <newName>)];

file - name of a script file to be imported (if file extension omitted '.cs' is assumed).
preserve_main - do not rename 'static Main' in the imported script
oldName - name of a namespace to be renamed during importing
newName - new name of a namespace to be renamed during importing

The shorter alias directive //css_imp can be used in place of the //css_import.

Another aliases are //css_include and //css_inc. They are equivalents of //css_import <file>, preserve_main.

If the string $this (or $this.name) is specified as a part of <file> it will be replaced at execution time with the main script full name (or file name only).

This feature was not in the original design of CS-Script but was added due to users demand.

File location

In the import statement the file to be imported can be specified with relative or absolute path. Try to avoid using the absolute path because it can create difficulties when isolating scripts for deployment.

If the script to be imported is specified only by the name without the path, the script file must be located in one of the following folders:

If a relative file path is specified with single-dot prefix it will be automatically converted into the absolute path with respect to the location of the file containing the directive being resolved. Otherwise it will be resolved with respect to the process current directory.

If for whatever reason it is preferred to always resolve path expression with respect to the parent script location you can configure the script engine to do it with the following command:

   cscs -config:set:ResolveRelativeFromParentScriptLocation=true

Note:

The <file> parameter can contain environment variable mask which would be expanded at runtime.
Remember that you may need to escape some path characters that conflict with the //css_ delimiters. See Delimiters Escaping section.

Example  

The file math.cs contains the following code:

using System;

namespace MathUtils
{
    class Calculator  
    {  
        static public int Add(int a, int b)
        {
            return a+b;
        }
    }
}
 

The following are examples of using the method 'Add' of a math.cs in another C# script:

//css_import math;
using
 System;

class Script
{
    static public void Main(string [] args)
    {
        Console.WriteLine(MathUtils.Calculator.Add(1,2));
    }
}


It is highly advisable to have namespaces declared in dependent scripts. Thus it would be possible to resolve naming conflicts should they arise. 

//css_import math, rename_namespace(MathUtils, MyMath);
using System;

class Script
{
    static public void Main(string [] args)
    {
        Console.WriteLine(MyMath.Calculator.Add(1,2));
    }
}

 
Note

It is important to remember that the concept of importing a script is different from what is called "include" in C++. Dependant script (script B) code cannot be just "appended" to the master script (script A) as this may produce C# code where static void Main()  is defined more than once. That is why the script engine always imports/injects adjusted copy of the dependent script into master script. 'Adjusted' means that if the script to be imported contains static void Main(..) it will be always renamed to i_Main.


The only case when the importing is a logical equivalent of "include" is when importable script does not have static void Main() (non-runable script) and no namespace renaming is requested (no rename_namespace parameter) or when renaming is disabled with preserve_main parameter.

See also

Script Library | Integration with OS | Script Importing (Tutorial)