Added asdCS-Script 2.4.1

CS-Script Runtume Environment

The script engine is a small runtime environment and as such it has some characteristics, which may be accessed/analysed at runtime. The CSScriptLibrary contains class CSSEnvironment, which provides information about the CS-Script runtime environment. Use the CSSEnvironment class to retrieve information such as:


Because CSSEnvironment interface may vary from version to version see CSScriptLibrary help in Programming Reference for details .

NOTE: ScriptFile and PrimaryScriptFile are made obsolete as they may not work correctly in the hosted and cached scenarios. Use alternative techniques demonstrated in theReflectScript.cs sample. Including environment variable 'EntryScript' and AssemblyDescriptionAttribute (of the script assembly) containing the full path of the script file.

The full name of the script being executed can also be discovered in a very simple way, which does not require loading any external dependency (CSScriptLibrary.dll). Every script being excuted has its assembly stamped with the AssemblyDescriptionAttribute value of which is set to the full script file name.

var scriptFile = assembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), true)

More samples on how obtain the script name from the script can be found in the ReflectScript sample (<cs-script>\Samples\ReflectScript.cs).

In addition to this the script engine always sets at startup some environment variables for current process:
This can be useful when you need to know at run time if the execution is under CS-Script environment and/or IDE. The following code illustrates this:

using System;

class Script
    static public void Main(string[] args)
        string cssRuntime = Environment.GetEnvironmentVariable("CSScriptRuntime");
        if (cssRuntime != null)
            Console.WriteLine("Running under CS-Script v" + cssRuntime.ToString());
            string cssDebugging = Environment.GetEnvironmentVariable("CSScriptDebugging");
            if (cssDebugging != null)
                Console.WriteLine("Debugging under " + cssDebugging.ToString());
            Console.WriteLine("Running as stand-alone application");

In addition to the environment variables you can also detect "under IDE" (e.g. Visual Studio) execution with the compiler symbol CSS_PROJECT:

        Console.WriteLine("Script is opened with Visual Studio.");

In script hosting scenarios it can be useful to distinguish "scripted"code from "static" one. This can be useful when you need to know at run time if the methed implemented by the host application is invoked by the Host or Script routine.  CS-Script engine stamps every compiled script assembly with the AssemblyDescriptionAttribute custom attribute containing the script file as attribute Description property. Thus you can always analyse your StackFrame and check if the calling assembly is the script being executed.  To simlify this task CSScriptLibrary implements Assembly extension method GetScriptName().

public void SomeHostMethod()
if (Assembly.GetCallingAssembly().GetScriptName() != null)
"Invoked from the script...");
        Console.WriteLine("Invoked from the host...");

See Also 

CS-Script application configuration