Exploring magic commands
Throughout this book, we’ve seen a number of magic commands, such as #r
, #!connect
, and #import
. In this section, we’ll discuss a few other magic commands and talk about how they are implemented at a high level. Put simply, magic commands are special methods triggered by unusual syntax, referred to as directives.
When a kernel in .NET Interactive encounters a registered directive, it follows the following approximate sequence:
- The directive is marked as ignored and not treated as code by the language kernel. In other words, the C# kernel won’t attempt to compile a
#r
magic command. - Any relevant parameters are identified and extracted from the command. For example, the
#r "nuget:Microsoft.ML"
command extracts the NuGet string as a parameter and errors if it is not specified. - .NET Interactive creates an instance of a relevant
KernelCommand
object and sets properties as needed. We’ll talk more about this...