Page 17 - MSDN Magazine, May 2017
P. 17
Figure 1 Visual Studio Code Needs to Update the Project
thesameconceptsapplytomacOSandLinux.Togetstarted,opena command prompt and move to a folder on your disk. For instance, say you have a folder called C:\Apps, go to this folder and create a new subfolder called RoslynCore, using the following commands:
> cd C:\Apps
> md RoslynCore > cd RoslynCore
Therefore, RoslynCore will be the name of the sample applica- tion discussed in this article. It will be a Console application, which is perfect for instructional purposes and simplifies the approach to coding with Roslyn. You can also use the same techniques in ASP.NET Core Web applications. To create a new, empty project for a Console application, simply type the following command line:
> dotnet new console
In this way, .NET Core scaffolds a C# project for a Console application called RoslynCore. Now you can open the project’s folder with Visual Studio Code. The easiest way is typing the following command line:
> code .
Of course, you could open Visual Studio Code from the Windows Start menu and then open a project folder manually. Once you enter any C# code file, it will ask your permission to generate some required assets and to restore some NuGet packages (see Figure 1).
The next step is adding the NuGet packages necessary for work- ing with Roslyn.
Adding the Roslyn NuGet Packages
As you might know, the Roslyn APIs can be consumed by installing some NuGet packages from the Microsoft.CodeAnalysis hierarchy. Before installing these packages, it’s important to clarify how the Roslyn APIs fit into the .NET Core system. If you’ve ever worked with Roslyn on the .NET Framework, you might be used to taking advantage of the full set of Roslyn APIs. .NET Core relies on .NET Standard libraries, which means that only the Roslyn libraries that support .NET Standard can be consumed in .NET Core. At the time of this writing, most of the Roslyn APIs are already available to .NET Core, including (but not limited to) the Compiler APIs msdnmagazine.com
(with the Emit and Diagnostic APIs) and the Workspaces APIs. Only a few APIs are not yet por- table, but Microsoft is investing hugely in Roslyn and .NET Core, so it’s reasonable to expect full .NET Standard compatibility in future releases. A real-world example of a cross-platform appli- cation that runs on .NET Core is OmniSharp (bit.ly/2mpcZeF), which leverages the Roslyn APIs to empower most of the code editor features, such as completion lists and syntax highlighting.
In this article, you’ll see how to leverage the Compiler and the Diagnostic APIs. To accom- plish this, you need to add the Microsoft.CodeAnalysis.CSharp
NuGetpackagetoyourproject.Withthenew.NETCoreproj- ect system based on MSBuild, the list of NuGet packages is now included in the .csproj project file. In Visual Studio 2017, you can use the client UI for NuGet to download, install and manage packages, but in Visual Studio Code there’s no equivalent option. Fortunately, you can simply open the .csproj file and locate the <ItemGroup> node that contains <PackageReference> elements, each representing a required NuGet package. Modify the node as follows:
<ItemGroup> ...
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.0.0 " />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" /> </ItemGroup>
Figure 2 Parsing Source Code and Retrieving a Syntax Node
using System;
using RoslynCore;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
class Program {
static void Main(string[] args) {
GenerateSampleViewModel(); }
static void GenerateSampleViewModel() {
const string models = @"namespace Models {
public class Item {
public string ItemName { get; set } }
} ";
Console.ReadLine(); }
}
var node = CSharpSyntaxTree.ParseText(models).GetRoot(); var viewModel = ViewModelGeneration.GenerateViewModel(node);
if(viewModel!=null) Console.WriteLine(viewModel.ToFullString());
May 2017 13