Useful resources to write Roslyn Analyzers

 
 
  • Gérald Barré

This post is part of the series 'Roslyn Analyzers'. Be sure to check out the rest of the blog posts of the series!

Roslyn analyzers and source generators are useful but it's not easy to start writing them. The documentation is not at the same level as .NET or ASP.NET Core. There are some good resources on the internet. In this post, I share some resources to help you write Roslyn analyzers and source generators.

#View Syntax tree and IOperation tree

When you write a Roslyn analyzer, you need to understand the syntax tree and the IOperation tree to see what you should analyze. Here are some tools to help you visualize them:

#Writing a syntax tree

When you write a Roslyn analyzer, you often need to write a code fix. It's not easy to write a code fix because you need to understand how to create a syntax tree. Roslyn Quoter (GitHub) transforms a given C# snippet into the syntax factory API calls to construct its syntax tree.

#Finding analyzer examples

The Roslyn documentation is not extensive. Sometimes, you can look at other Roslyn analyzer's implementations to find how to write your own. Here are some resources to find Roslyn analyzers:

  • Roslyn Source Browser: Navigate into Roslyn source code.
  • grep.app: Find code on GitHub. You can search for specific Roslyn methods or attributes to see how other projects use them.
  • dotnet/roslyn-analyzers: Contains all the CA rules. There are lots of analyzers to look at!

#Which version of Roslyn is associated with which version of Visual Studio?

If you want to determine the minimal version of Roslyn to use in your analyzer, you can refer to the Roslyn version support documentation.

#Other resources

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub