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:

Properties

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

The full name of the script being executed can also be discovered in a very simple way , which dows nor 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:
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" 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 CSScriptLibrary.ScriptedCodeAttribute custom attribute. 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 IsScriptAssembly.

public void SomeHostMethod()
{
    
if (Assembly.GetCallingAssembly().IsScriptAssembly())
        Console.WriteLine(
"Invoked from the script...");
    
if
        Console.WriteLine("Invoked from the host...");
}


See Also 

CS-Script application configuration