using Mono.CSharp; var script = new ToplevelBlock(new EvaluatorContext()); script.AddReferencesFromAssembly(typeof(Console).Assembly); script.Compile(@" Console.WriteLine(""Hello, World!""); "); script.Run();
using Mono.CSharp; var script = new ToplevelBlock(new EvaluatorContext()); script.AddReferencesFromAssembly(typeof(Math).Assembly); script.Compile(@" int x = 5; Console.WriteLine(Math.Sqrt(x)); "); var result = script.Run(); int returnValue = (int)result.ReturnValue;This example sets up a ToplevelBlock to evaluate a script that calculates the square root of 5 using the Math.Sqrt method. It then retrieves the return value of the script, which in this case is the integer value of the square root, and assigns it to a variable. Overall, Mono.CSharp is a powerful package library that allows for dynamic code execution and interactive scripting in C#. The ToplevelBlock class is just one of the many useful components it provides for this purpose.