How to Evaluate C# Expressions Dynamically

Naila Saad Siddiqui Feb 02, 2024
  1. Use the Roslyn Scripting API to Evaluate C# Expressions Dynamically
  2. Steps for Using the Scripting API to Evaluate C# Expressions Dynamically
How 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.

  1. It is a piece of technology that converts .NET source code into an executable binary.
  2. Its primary purpose relates to IDEs (Integrated Development Environments) and other tool development.
  3. 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:

  1. For the evaluation of C# expressions.
  2. For adding some references or importing any library at the runtime.
  3. For parameterizing a script.
  4. For executing a script multiple times.
  5. For the creation of a delegate of the script.
  6. 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:

step 1 for the installation of API

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:

output of the expression evaluation

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();
  }
}

output of the evaluate expression

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.