Compiler: A compiler is a program that converts source code (original language) written in one programming language into another (target language). Compilation is the process of translating from source code (usually a high-level language) to object code (usually a low-level language or machine language) that can be executed directly by a computer or virtual machine.
In the .NET platform, there are two different compilers at different stages of executing the model: one called the Roslyn compiler, which compiles C# and VB code into assemblies; The other is called the RyuJIT compiler, which compiles the IL (intermediate language) code in the assembly into machine code.
This article starts with the Roslyn compiler. We don't have to delve into how it works, but to understand how it works, to know what it can be used for.
Initially, the compiler of the C# language was written in C++, and later Microsoft introduced a new compiler written in C# itself: Roslyn, which is a bootstrap compiler.
The bootstrap compiler means that the compiler of a certain programming language is written in the language itself. Each version of the bootstrap compiler is compiled with a version that preceded it, but its first version must be compiled by a compiler written in another language, such as Roslyn's first version compiled by a compiler written in C++. Many programming languages mature and write their own compilers using the language itself, such as C# and Go.
On the .NET platform, the Roslyn compiler is responsible for compiling C# and VB code into assemblies.
Most existing traditional compilers are "black box" patterns that convert source code into executables or library files, and we have no way of knowing what happens in between. Unlike Roslyn, Roslyn allows you to access every stage of the code compilation process through APIs.
Its working mechanism is pipeline-based, with four stages, each of which is an independent module, and each module provides a corresponding API. Integrated development environments (IDEs) can leverage these APIs to provide convenient tools to improve development efficiency, such as code highlighting, smart hints, refactoring tools, performance analysis tools, and more. In addition, with Roslyn, developers can use the compiler as a service in their own programs.
Leverage the APIs provided by Roslyn to dynamically generate code samples
First, create a new ClassLibraryGenerator library and create a .NET standard library project that targets the netstandard2.0 target framework moniker (TFM). Add the NuGet packages Microsoft.CodeAnalysis.Analyzers and Microsoft.CodeAnalysis.CSharp, csproj is configured as follows:
Create a new C# file called TestSourceGenerator.cs that specifies your own source generator as follows:
From the context object, we can access the compiled entry point or the Main method. A mainMethod instance is an IMethodSymbol that represents the symbol of a method or similar method (including constructors, destructors, operators, or attribute/event accessors). The Microsoft.CodeAnalysis.Compilation.GetEntryPoint method returns the IMethodSymbol of the program's entry point. Other methods allow you to find any method symbol in your project. In this object, we can deduce the namespace (if it exists) and the type it contains. The source in this example is an interpolated string that templates the source code to be generated, with the interpolated gaps populating the contained namespace and type information. Add source to context with the prompt name. For this example, the builder creates a new build source file that contains the implementation of the partial method in the console application. A source generator can be written to add any favorite source.
Create a new ConsoleApp4 console app (without top-level statements) with the following code:
At the same time, add the ClassLibraryGenerator project reference, as follows:
Try launching the console app with the output as follows:
In Visual Studio, click on the project "Dependencies" - > "Analyzer" - > "ClassLibraryGenerator" - > "ClassLibraryGenerator.TestSourceGenerator", double-click on the "Program.g.cs" file to see the generated code, as follows:
Prompt:Modifying the builder's code may require a restart of Visual Studio to see it。
Roslyn GitHub address:The hyperlink login is visible.
Reference:
The hyperlink login is visible.
The hyperlink login is visible.
The hyperlink login is visible. |