Using the Roslyn scripting API to execute C# code snippets
In this section, we will show you how to to write a C# console application that uses Roslyn scripting APIs to execute C# code snippets and consume their output. The scripting APIs enable .NET applications to instantiate a C# engine and execute code snippets against host-supplied objects. The scripting APIs can also be used directly in an interactive session.
Getting started
You will need to have the Visual Studio 2017 Community edition installed on your machine to execute this recipe. You can install a free community edition from https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Community&rel=15.
How to do it...
- Open Visual Studio and create a new C# console application targeting .NET Framework 4.6 or higher, say
ConsoleApp
. - Install the
Microsoft.CodeAnalysis.CSharp.Scripting
NuGet package (at the time of writing, the latest stable version is 2.1.0). For guidance on how to search for and install the NuGet package...