Compilers build a detailed model of application code as they validate the
syntax and semantics of that code. They use this model to build the
executable output from the source code. The .NET Compiler Platform SDK provides
access to this model. Increasingly, we rely on integrated
development environment (IDE) features such as IntelliSense, refactoring,
intelligent rename, "Find all references," and "Go to definition" to
increase our productivity. We rely on code analysis tools to improve our
code quality, and code generators to aid in application construction. As
these tools get smarter, they need access to more and more of the model
that only compilers create as they process application code. This is the core mission of
the Roslyn APIs: opening up the opaque boxes and allowing tools and end
users to share in the wealth of information compilers have about our code.
Instead of being opaque source-code-in and object-code-out translators,
through Roslyn, compilers become platforms: APIs that you can use for
code-related tasks in your tools and applications.
.NET Compiler Platform SDK concepts
The .NET Compiler Platform SDK dramatically lowers the barrier
to entry for creating code focused tools and applications. It creates many
opportunities for innovation in areas such as meta-programming, code
generation and transformation, interactive use of the C# and Visual Basic languages,
and embedding of C# and Visual Basic in domain-specific languages.
The .NET Compiler Platform SDK enables you to build analyzers and
code fixes that find and correct coding mistakes. Analyzers
understand the syntax (structure of code) and semantics to detect practices that
should be corrected. Code fixes provide one or more suggested fixes
for addressing coding mistakes found by analyzers or compiler diagnostics. Typically, an analyzer
and the associated code fixes are packaged together in a single project.
Analyzers and code fixes use static analysis to understand code. They
do not run the code or provide other testing benefits. They can, however,
point out practices that often lead to bugs, unmaintainable code, or
standard guideline violation.
In addition to analyzers and code fixes, The .NET Compiler Platform SDK also enables you to build code refactorings.
It also provides a single set of APIs that enable
you to examine and understand a C# or Visual Basic codebase. Because you
can use this single codebase, you can write analyzers and code fixes more
easily by leveraging the syntactic and semantic analysis APIs provided by
the .NET Compiler Platform SDK. Freed from the large task of replicating
the analysis done by the compiler, you can concentrate on the more focused
task of finding and fixing common coding errors for your project or library.
A smaller benefit is that your analyzers and code fixes are smaller and
use much less memory when loaded in Visual Studio than they would
if you wrote your own codebase to understand the code in a project. By
leveraging the same classes used by the compiler and Visual Studio, you
can create your own static analysis tools. This means your team
can use analyzers and code fixes without a noticeable impact on the IDE's
performance.
There are three main scenarios for writing analyzers and code fixes:
Many teams have coding standards that are enforced through code reviews
with other team members. Analyzers and code fixes can make this process
much more efficient. Code reviews happen when a developer shares their work
with others on the team. The developer will have invested all the time needed to
complete a new feature before getting any comments. Weeks may go by
while the developer reinforces habits that don't match the team's practices.
Analyzers run as a developer writes code. The developer gets immediate feedback that
encourages following the guidance immediately. The developer builds habits to write
compliant code as soon as they begin prototyping. When the feature is
ready for humans to review, all the standard guidance has been enforced.
Teams can build analyzers and code fixes that look for the most common
practices that violate team coding practices. These can be installed on
each developer's machine to enforce the standards.
Suġġeriment
Before building your own analyzer, check out the built-in ones. For more information, see Code-style rules.
Provide guidance with library packages
There is a wealth of libraries available for .NET developers on NuGet.
Some of these come from Microsoft, some from third-party companies, and
others from community members and volunteers. These libraries get more
adoption and higher reviews when developers can succeed with those
libraries.
In addition to providing documentation, you can provide analyzers and
code fixes that find and correct common mis-uses of your library. These
immediate corrections will help developers succeed more quickly.
You can package analyzers and code fixes with your library on NuGet. In that
scenario, every developer who installs your NuGet package will also install
the analyzer package. All developers using your library will immediately
get guidance from your team in the form of immediate feedback on mistakes
and suggested corrections.
Provide general guidance
The .NET developer community has discovered, through experience, patterns that
work well and patterns that are best avoided. Several community members
have created analyzers that enforce those recommended patterns. As we learn
more, there is always room for new ideas.
These analyzers can be uploaded to the
Visual Studio Marketplace and downloaded
by developers using Visual Studio. Newcomers to the language and the platform
learn accepted practices quickly and become productive earlier in their .NET
journey. As these become more widely used, the community adopts these
practices.
Source generators
Source generators aim to enable compile time metaprogramming, that is, code that can be created
at compile time and added to the compilation. Source generators are able to read the contents of
the compilation before running, as well as access any additional files. This ability enables them to
introspect both user C# code and generator-specific files. You can learn how to build incremental
source generators using the source generator cookbook.
Next steps
The .NET Compiler Platform SDK includes the latest language object models
for code generation, analysis, and refactoring. This section provides a
conceptual overview of the .NET Compiler Platform SDK. Further details can be
found in the quickstarts, samples, and tutorials sections.
You can learn more about the concepts in the .NET Compiler Platform SDK in these five topics:
To get started, you'll need to install the .NET Compiler Platform SDK:
Installation instructions - Visual Studio Installer
There are two different ways to find the .NET Compiler Platform SDK in the Visual Studio Installer:
Install using the Visual Studio Installer - Workloads view
The .NET Compiler Platform SDK is not automatically selected as part of the Visual Studio extension development workload. You must select it as an optional component.
Run Visual Studio Installer
Select Modify
Check the Visual Studio extension development workload.
Open the Visual Studio extension development node in the summary tree.
Check the box for .NET Compiler Platform SDK. You'll find it last under the optional components.
Optionally, you'll also want the DGML editor to display graphs in the visualizer:
Open the Individual components node in the summary tree.
Check the box for DGML editor
Install using the Visual Studio Installer - Individual components tab
Run Visual Studio Installer
Select Modify
Select the Individual components tab
Check the box for .NET Compiler Platform SDK. You'll find it at the top under the Compilers, build tools, and runtimes section.
Optionally, you'll also want the DGML editor to display graphs in the visualizer:
Check the box for DGML editor. You'll find it under the Code tools section.
Ikkollabora magħna fuq GitHub
Is-sors għal dan il-kontenut jista’ jinstab fuq GitHub, fejn tista’ wkoll toħloq u tirrevedi l-problemi u t-talbiet għall-immerġjar. Għal aktar informazzjoni, ara l-gwida għall-kontributuri tagħna.
Feedback ta’ .NET
.NET huwa proġett b’sors miftuħ. Agħżel link biex tipprovdi l-feedback: