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:
Properties
- Settings
csscript.Settings object containing the data from the script engine custom configuration file (eg. css_config.xml).
- CacheDirectory
Location of all cache and temporary files (auto-generated by the script engine) for the script file being executed.
- ScriptFile (obsolete)
The name of the script file being executed.
- PrimaryScriptFile (obsolete)
The name of the primary script file. It is the same as the name of the script being executed, except for the scripts, which are subject of the pre/post execution actions. In this case it is the name of the parent script containing pre/post execution directives.
Methods
- string GetCacheDirectory(string file)
Returns the name of the cache directory for the given script file.
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) .Cast<AssemblyDescriptionAttribute>() .First() .Description;
|
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:
- CSScriptRuntime - version of the script engine.
- CSScriptRuntimeLocation - location of the script engine.
- CSScriptDebugging - name and version of the debugger/IDE
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());
}
}
else
{
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:
#if CSS_PROJECT
Console.WriteLine("Script is opened with Visual Studio.");
#endif
|
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)
Console.WriteLine("Invoked from the script...");
else
Console.WriteLine("Invoked from the host...");
} |
CS-Script application configuration