Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
vermeir_nico_introducing_net_6_getting_started_with_blazor_m.pdf
Скачиваний:
19
Добавлен:
26.06.2023
Размер:
11.64 Mб
Скачать

Chapter 10 .NET Compiler Platform

Note that you might need to restart Visual Studio before Intellisense recognizes the generated objects.

Debugging Source Generators

As you might have guessed, debugging source generators is not as simple as setting a breakpoint and hitting the run button, but it is not much harder either. Instead of placing breakpoints, we can use the Debugger class from the System.Diagnostics namespace to programmatically pause the generator’s execution. Listing 10-16 shows the statement right at the start of code generation.

Listing 10-16.  Debugging a source generator

public void Execute(GeneratorExecutionContext context)

{

Debugger.Launch();

If we trigger the source generator again, by rebuilding the program that uses the generator, the message in Figure 10-12 will pop up.

293

Chapter 10 .NET Compiler Platform

Figure 10-12.  Selecting a debugger

Select New instance of Visual Studio 2022. VS2022 will start up, load in the source file for the generator, and pause right at the Debugger.Launch statement, just like if it was

a breakpoint. From this point on, we are in debug mode; we can inspect variables, step over or into statements, and so on. The Debugger.Launch call can be placed anywhere in the generator, even in a syntax receiver.

294