Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

The .NET Compiler Platform SDK

Feedback

In this article

Compilers build a detailed model of application code as they validate thesyntax and semantics of that code. They use this model to build theexecutable output from the source code. The .NET Compiler Platform SDK providesaccess to this model. Increasingly, we rely on integrateddevelopment environment (IDE) features such as IntelliSense, refactoring,intelligent rename, "Find all references," and "Go to definition" toincrease our productivity. We rely on code analysis tools to improve ourcode quality, and code generators to aid in application construction. Asthese tools get smarter, they need access to more and more of the modelthat only compilers create as they process application code. This is the core mission ofthe Roslyn APIs: opening up the opaque boxes and allowing tools and endusers 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 forcode-related tasks in your tools and applications.

.NET Compiler Platform SDK concepts

The .NET Compiler Platform SDK dramatically lowers the barrierto entry for creating code focused tools and applications. It creates manyopportunities for innovation in areas such as meta-programming, codegeneration 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 buildanalyzers andcode fixes that find and correct coding mistakes.Analyzersunderstand the syntax (structure of code) and semantics to detect practices thatshould be corrected.Code fixes provide one or more suggested fixesfor addressing coding mistakes found by analyzers or compiler diagnostics. Typically, an analyzerand the associated code fixes are packaged together in a single project.

Analyzers and code fixes use static analysis to understand code. Theydo not run the code or provide other testing benefits. They can, however,point out practices that often lead to bugs, unmaintainable code, orstandard guideline violation.

In addition to analyzers and code fixes, The .NET Compiler Platform SDK also enables you to buildcode refactorings.It also provides a single set of APIs that enableyou to examine and understand a C# or Visual Basic codebase. Because youcan use this single codebase, you can write analyzers and code fixes moreeasily by leveraging the syntactic and semantic analysis APIs provided bythe .NET Compiler Platform SDK. Freed from the large task of replicatingthe analysis done by the compiler, you can concentrate on the more focusedtask of finding and fixing common coding errors for your project or library.

A smaller benefit is that your analyzers and code fixes are smaller anduse much less memory when loaded in Visual Studio than they wouldif you wrote your own codebase to understand the code in a project. Byleveraging the same classes used by the compiler and Visual Studio, youcan create your own static analysis tools. This means your teamcan use analyzers and code fixes without a noticeable impact on the IDE'sperformance.

There are three main scenarios for writing analyzers and code fixes:

  1. Enforce team coding standards
  2. Provide guidance with library packages
  3. Provide general guidance

Enforce team coding standards

Many teams have coding standards that are enforced through code reviewswith other team members. Analyzers and code fixes can make this processmuch more efficient. Code reviews happen when a developer shares their workwith others on the team. The developer will have invested all the time needed tocomplete a new feature before getting any comments. Weeks may go bywhile the developer reinforces habits that don't match the team's practices.

Analyzers run as a developer writes code. The developer gets immediate feedback thatencourages following the guidance immediately. The developer builds habits to writecompliant code as soon as they begin prototyping. When the feature isready for humans to review, all the standard guidance has been enforced.

Teams can build analyzers and code fixes that look for the most commonpractices that violate team coding practices. These can be installed oneach developer's machine to enforce the standards.

Tip

Before building your own analyzer, check out the built-in ones. For more information, seeCode-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, andothers from community members and volunteers. These libraries get moreadoption and higher reviews when developers can succeed with thoselibraries.

In addition to providing documentation, you can provide analyzers andcode fixes that find and correct common mis-uses of your library. Theseimmediate corrections will help developers succeed more quickly.

You can package analyzers and code fixes with your library on NuGet. In thatscenario, every developer who installs your NuGet package will also installthe analyzer package. All developers using your library will immediatelyget guidance from your team in the form of immediate feedback on mistakesand suggested corrections.

Provide general guidance

The .NET developer community has discovered, through experience, patterns thatwork well and patterns that are best avoided. Several community membershave created analyzers that enforce those recommended patterns. As we learnmore, there is always room for new ideas.

These analyzers can be uploaded to theVisual Studio Marketplace and downloadedby developers using Visual Studio. Newcomers to the language and the platformlearn accepted practices quickly and become productive earlier in their .NETjourney. As these become more widely used, the community adopts thesepractices.

Source generators

Source generators aim to enablecompile time metaprogramming, that is, code that can be createdat compile time and added to the compilation. Source generators are able to read the contents ofthe compilation before running, as well as access anyadditional files. This ability enables them tointrospect both user C# code and generator-specific files. You can learn how to build incrementalsource generators using thesource generator cookbook.

Next steps

The .NET Compiler Platform SDK includes the latest language object modelsfor code generation, analysis, and refactoring. This section provides aconceptual overview of the .NET Compiler Platform SDK. Further details can befound 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 theVisual 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.

  1. RunVisual Studio Installer
  2. SelectModify
  3. Check theVisual Studio extension development workload.
  4. Open theVisual Studio extension development node in the summary tree.
  5. Check the box for.NET Compiler Platform SDK. You'll find it last under the optional components.

Optionally, you'll also want theDGML editor to display graphs in the visualizer:

  1. Open theIndividual components node in the summary tree.
  2. Check the box forDGML editor

Install using the Visual Studio Installer - Individual components tab

  1. RunVisual Studio Installer
  2. SelectModify
  3. Select theIndividual components tab
  4. Check the box for.NET Compiler Platform SDK. You'll find it at the top under theCompilers, build tools, and runtimes section.

Optionally, you'll also want theDGML editor to display graphs in the visualizer:

  1. Check the box forDGML editor. You'll find it under theCode tools section.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?