Page 16 - MSDN Magazine, May 2017
P. 16
.NET CORE
Cross-Platform Code
Generation with Roslyn
and .NET Core
Alessandro Del Sole
.NET Core is the modular, open source and cross-platform set of tools that allows you to build next-generation .NET appli- cations, which run on Windows, Linux and macOS (microsoft.com/ net/core/platform). It can also be installed on the Windows 10 for IoT distribution, and it runs on devices such as the Raspberry PI. .NET Core is a powerful platform that includes the runtime, libraries, and compilers, with full support for languages such as C#, F#, and Visual Basic. This means you can code in C# not only on Windows, but also on different OSes because the .NET Compiler Platform (github.com/dotnet/roslyn), also referred to as “Project Roslyn,” provides open source, cross-platform compilers with rich code analysis APIs. As an important implication, you can leverage the Roslyn APIs to perform many code-related operations on different OSes, such as code analysis, code generation and compilation. This article walks through the necessary steps to set up a C# project on .NET Core to use the Roslyn APIs and explains some interesting code-generation and compilation scenarios. It also discusses some basic Reflec- tion techniques to invoke and run code compiled with Roslyn on .NET Core. If you’re unfamiliar with Roslyn, you’ll want to read the following articles first:
• “Use Roslyn to Write a Live Code Analyzer for Your API” (msdn.com/magazine/dn879356)
• “Adding a Code Fix to Your Roslyn Analyzer” (msdn.com/magazine/dn904670)
• “Maximize Your Model-View-ViewModel Experience with Roslyn” (msdn.com/magazine/mt703435).
Installing the .NET Core SDK
The first step is installing .NET Core and the SDK. If you work on Windows and you’ve installed Visual Studio 2017, .NET Core is already included if the .NET Core cross-platform development work- load was selected at installation time in the Visual Studio Installer. If not, simply open the Visual Studio Installer, select the workload and click Modify. If you’re working on Windows but not relying on Visual Studio 2017, or you’re working on Linux or macOS, you can install .NET Core manually and use Visual Studio Code as the devel- opment environment (code.visualstudio.com). The latter is the scenario I’ll discuss in this article, as Visual Studio Code is cross-platform itself; thus, it’s a great companion for .NET Core. Also, remember to install the C# extension for Visual Studio Code (bit.ly/29b1Ppl). The steps to install .NET Core are different depending on the OS, so fol- low the instructions at bit.ly/2mJArWx. Make sure you install the latest release. It’s worth mentioning that the latest releases of .NET Core no longer support the project.json file format, but instead support the more common .csproj file format with MSBuild.
Scaffolding a .NET Core Application in C#
With .NET Core, you can create Console applications and Web appli- cations. For Web applications, Microsoft is making more templates available, besides the ASP.NET Core template, as .NET Core goes forward on its roadmap. Because Visual Studio Code is a lightweight editor, it doesn’t provide project templates as Visual Studio does. This means you need to create an application from the command line inside a folder whose name will also be the application name. The following example is based on instructions for Windows, but
This article discusses:
• Building a .NET Core application in C# • Adding the Roslyn NuGet packages
• Code analysis and code generation
• Getting diagnostic information
• Compiling and executing code on-the-fly with the Emit APIs Technologies discussed:
C#, .NET Core, Roslyn, Visual Studio 2017, Visual Studio Code
12 msdn magazine