How to Evaluate C# Expressions Dynamically
- Use the Roslyn Scripting API to Evaluate C# Expressions Dynamically
- Steps for Using the Scripting API to Evaluate C# Expressions Dynamically
This trivial guide is about the use of scripting API to evaluate the C# expressions dynamically at runtime. For this purpose, we will use and learn Roslyn Scripting API.
Use the Roslyn Scripting API to Evaluate C# Expressions Dynamically
Roslyn is a platform for .NET compilers with code analysis APIs and compilers for .NET languages.
- It is a piece of technology that converts .NET source code into an executable binary.
- Its primary purpose relates to IDEs (Integrated Development Environments) and other tool development.
- Additionally, it includes features for the scripting API that let you add user-written code execution at runtime.
The scripting APIs allow .NET applications to launch a C# engine and run code fragments against objects provided by the host. Roslyn scripting API can be used for the following purposes:
- For the evaluation of C# expressions.
- For adding some references or importing any library at the runtime.
- For parameterizing a script.
- For executing a script multiple times.
- For the creation of a delegate of the script.
- Development and analyze a C# script.
Steps for Using the Scripting API to Evaluate C# Expressions Dynamically
To start with scripting, you first need to install the scripting API in your C# project.
Installation of API
For the installation of API, first, create a new Console Application in Visual Studio. After the application is created and loaded in the IDE, go to Tools->NuGet Package Manager->Manage NuGet Packages for the Solution
. The following window will pop up:
In the window, head to the Browse tab and search for CSharp Scripting. Then install any of the highlighted packages.
After the package installation, head to your Program.cs
file.
Include the Library Files
After the installation of the package, you need to include the required library files in your code file like this:
using Microsoft.CodeAnalysis.CSharp.Scripting;
Write the Code to Evaluate the Expression
The EvaluateAsync()
method is used to evaluate the script. This method takes in a string containing the expression to be evaluated and returns a string containing the result of that expression.
This is the only function that is used in the library.
static void Main(string[] args) {
Console.WriteLine("Welcome to C# Scripting");
Console.WriteLine("Enter the code to run as a C# Script");
var command = Console.ReadLine();
var results = CSharpScript.EvaluateAsync(command).Result;
Console.WriteLine(results);
}
In the above code segment, we have taken the expression as input from the user and then passed it to the EvaluateAsync()
method. This method returns the result in the result
variable, printed on the screen.
This will give the following output:
We can modify the code so the program keeps asking for the expressions as input until the user enters exit
and displays the output. For this, we will use a while
loop and take the input in that loop.
Let’s look at the code segment for this implementation:
static void Main(string[] args) {
Console.WriteLine("Welcome to C# Scripting");
Console.WriteLine("Enter the code to run as a C# Script");
var command = Console.ReadLine();
while (command.ToString() != "exit") {
var results = CSharpScript.EvaluateAsync(command).Result;
Console.WriteLine(results);
command = Console.ReadLine();
}
}
You can see from the output window above that the program takes input and gives output spontaneously. When the user enters the exit
, it exits the code.
Such types of scripts are used for unit testing of many desktop applications.