Running the script
The execution of a script is initiated by the Script.RunMain
method. This method scans the types declared in the script
(the list of types is available in the Script.Types
property). It looks for a type that has a static method Main
,
and passes control to this method.
In the example below, running the script using the RunMain
method will find the TestClass
type, which has a static Main
method,
and execute it:
using FastScript.CSharp;
var text =
@"
namespace Test
{
public class TestClass
{
public static void Main()
{
System.Console.WriteLine(""Hello!"");
}
}
}
";
var script = new CSharpScript();
if (script.Compile(text))
{
script.RunMain();
}
If the Main
method being run has parameters, their values must be specified in the RunMain
method, for example:
// script method:
// public static void Main(int id, string text)
script.RunMain(1, "abc");
The RunMain
method returns the value returned by the Main
method declared in the script. If the Main
method is declared as void
,
the return value is undefined.
If the script has top level statements, a special class is created for them that has a static Main
method.
When the script is run using the RunMain
method, this method receives control.
The following is an example of a script similar to the one described above that uses top level statements:
using FastScript.CSharp;
var text =
@"
System.Console.WriteLine(""Hello!"");
";
var script = new CSharpScript();
if (script.Compile(text))
{
script.RunMain();
}