O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Roslyn Cookbook

Book Description

Use Roslyn as a service to write powerful extensions and tools and use them in Visual Studio to improve code quality and maintain your source code more effectively.

About This Book

  • Use Roslyn extensions and tools in Visual Studio to enforce "house rules" on code and fix security and performance vulnerabilities in your code.
  • Write Roslyn extensions using the Roslyn service API to help developers enforce conventions and design idioms.
  • Improve developer productivity by using Roslyn-based agile development features in Visual Studio, such as live unit testing, C# interactive and scripting.
  • Contribute to the C# language and compiler tool chain to analyze and edit code.

Who This Book Is For

.NET Developers and architects, who are interested in taking full advantage of the Roslyn based extensions and tools to improve the development processes, will find this book useful. Roslyn contributors, i.e. the producers and C# community developers, will also find this book useful

What You Will Learn

  • Write extensions to analyze source code and report warnings and errors.
  • Edit C# source code to fix compiler/analyzer diagnostics or refactor source code.
  • Improve code maintenance and readability by using analyzers and code fixes.
  • Catch security and performance issues by using PUMA scan analyzers and FxCop analyzers.
  • Perform Live Unit tests in Visual Studio.
  • Use C# interactive and scripting in Visual Studio.
  • Design a new C# language feature and implement various compiler phases for a new language feature.
  • Write command line tools to analyze and edit C# code.

In Detail

Open-sourcing the C# and Visual Basic compilers is one of the most appreciated things by the .NET community, especially as it exposes rich code analysis APIs to analyze and edit code. If you want to use Roslyn API to write powerful extensions and contribute to the C# developer tool chain, then this book is for you. Additionally, if you are just a .NET developer and want to use this rich Roslyn-based functionality in Visual Studio to improve the code quality and maintenance of your code base, then this book is also for you.

This book is divided into the following broad modules:

  1. Writing and consuming analyzers/fixers (Chapters 1 - 5): You will learn to write different categories of Roslyn analyzers and harness and configure analyzers in your C# projects to catch quality, security and performance issues. Moving ahead, you will learn how to improve code maintenance and readability by using code fixes and refactorings and also learn how to write them.
  2. Using Roslyn-based agile development features (Chapters 6 and 7): You will learn how to improve developer productivity in Visual Studio by using features such as live unit testing, C# interactive and scripting.
  3. Contributing to the C# language and compiler tool chain (Chapters 8 - 10): You will see the power of open-sourcing the Roslyn compiler via the simple steps this book provides; thus, you will contribute a completely new C# language feature and implement it in the Roslyn compiler codebase. Finally, you will write simple command line tools based on the Roslyn service API to analyze and edit C# code.

Style and approach

This book takes a recipe-based approach, teaching you how to perform various hacks with the Compiler API in your hands.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Preface
    1. What this book covers
    2. What you need for this book
    3. Who this book is for
    4. Conventions
    5. Reader feedback
    6. Customer support
      1. Downloading the example code
      2. Downloading the color images of this book
      3. Errata
      4. Piracy
      5. Questions
  2. Writing Diagnostic Analyzers
    1. Introduction
    2. Creating, debugging, and executing an analyzer project in Visual Studio
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Creating a symbol analyzer to report issues about symbol declarations
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There s more...
      5. See also
    4. Creating a syntax node analyzer to report issues about language syntax
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating a syntax tree analyzer to analyze the source file and report syntax issues
      1. Getting ready
      2. How to do it...
      3. How it works...
    6. Creating a method body analyzer to analyze whole method and report issues
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Creating a compilation analyzer to analyze whole compilation and report issues
      1. Getting ready
      2. How to do it...
      3. How it works...
    8. Writing unit tests for an analyzer project
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    9. Publishing NuGet package and VSIX for an analyzer project
      1. Getting ready
      2. How to do it...
  3. Consuming Diagnostic Analyzers in .NET Projects
    1. Introduction
    2. Searching and installing analyzers through the NuGet package manager
      1. Getting ready
      2. How to do it...
    3. Searching and installing VSIX analyzers through the VS extension gallery
      1. Getting ready
      2. How to do it...
    4. Viewing and configuring analyzers in solution explorer in Visual Studio
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Using the ruleset file and Rule Set editor to configure analyzers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
  4. Writing IDE Code Fixes, Refactorings, and Intellisense Completion Providers
    1. Introduction
    2. Creating, debugging, and executing a CodeFixProvider to fix a compiler warning
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Applying batch code fixes (FixAll) across different scopes: document, project, and solution
      1. Getting ready
      2. How to do it...
    4. Creating a custom FixAllProvider to fix all occurrences of an issue across a scope
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Creating a CodeRefactoringProvider to refactor source code to recommend using C# 7.0 tuples
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There s more...
    6. Creating a CompletionProvider to provide additional intellisense items while editing code.
      1. Getting ready
      2. How to do it...
      3. How it works...
    7. Writing unit tests for a CodeFixProvider
      1. Getting ready
      2. How to do it...
      3. How it works...
  5. Improving Code Maintenance of C# Code Base
    1. Introduction
    2. Configuring C# code style rules built into Visual Studio 2017
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There is more...
    3. Using the .editorconfig file for configuration of code style rules
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Using the public API analyzer for API surface maintenance
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Using third-party StyleCop analyzers for code style rules
      1. Getting ready
      2. How to do it...
      3. How it works...
  6. Catch Security Vulnerabilities and Performance Issues in C# Code
    1. Introduction
    2. Identifying configuration-related security vulnerabilities in web applications
      1. Getting ready
      2. How to do it...
      3. How it works...
    3. Identifying cross-site scripting vulnerabilities in view markup files (.cshtml, .aspx files) in web applications
      1. Getting ready
      2. How to do it...
      3. How it works...
    4. Identifying insecure method calls that can lead to SQL and LDAP injection attacks
      1. Getting ready
      2. How to do it...
      3. How it works...
    5. Identifying weak password protection and management in web applications
      1. Getting ready
      2. How to do it...
    6. Identifying weak validation of data from external components to prevent attacks such as cross-site request forgery and path tampering
      1. Getting ready
      2. How to do it...
    7. Identifying performance improvements to source code using FxCop analyzers
      1. Getting ready
      2. How to do it...
      3. How it works...
  7. Live Unit Testing in Visual Studio Enterprise
    1. Introduction
    2. Running live unit tests in Visual Studio for unit test projects based on NUnit, XUnit, and MSTest frameworks
      1. Getting started
      2. How to do it
    3. Viewing and navigating live unit test results
      1. Getting started
      2. How to do it...
    4. Understanding incremental live unit test execution with code changes
      1. Getting started
      2. How to do it...
      3. How it works
    5. Understanding Start/Stop/Pause/Continue/Restart functionality for fine grain control of LUT
      1. Getting started
      2. How to do it...
    6. Including and excluding subset of tests for live execution
      1. Getting started
      2. How to do it...
    7. Configuring different options for live unit testing using the Tools Options dialog
      1. Getting started
      2. How to do it...
  8. C# Interactive and Scripting
    1. Introduction
    2. Writing a simple C# script and evaluating it within the Visual Studio interactive window
      1. Getting started
      2. How to do it...
      3. How it works...
    3. Using script directives and REPL commands in the C# interactive window
      1. Getting started
      2. How to do it...
    4. Using keyboard shortcuts for evaluating and navigating through script sessions in the C# interactive window
      1. Getting started
      2. How to do it...
    5. Initializing the C# interactive session from the existing C# project
      1. Getting started
      2. How to do it...
    6. Executing the C# script on a Visual Studio developer command prompt using csi.exe
      1. Getting started
      2. How to do it...
    7. Using the Roslyn scripting API to execute C# code snippets
      1. Getting started
      2. How to do it...
      3. How it works...
  9. Contribute Simple Functionality to Roslyn C# Compiler Open Source Code
    1. Introduction
    2. Setting up Roslyn enlistment
      1. Getting Started
      2. How to do it...
    3. Implementing a new syntax error in the C# compiler code base
      1. Getting Started
      2. How to do it...
      3. How it works...
    4. Implementing a new semantic error in the C# compiler code base
      1. Getting Started
      2. How to do it...
      3. How it works...
    5. Writing unit tests for a new error in the C# compiler code base
      1. Getting Started
      2. How to do it...
    6. Using Roslyn Syntax Visualizer to view Roslyn syntax tokens and nodes for a source file
      1. Getting Started
      2. How to do it...
    7. Sending a Roslyn pull request to contribute to next version of C# compiler and VS IDE
      1. Getting Started
      2. How to do it...
  10. Design and Implement a New C# Language Feature
    1. Introduction
      1. New language feature: Switch Operator (?::)
    2. Designing syntax and grammar for a new C# language feature
      1. Getting Started
      2. How to do it...
      3. How it works...
    3. Implementing parser support for a new C# language feature
      1. Getting Started
      2. How to do it...
      3. How it works...
    4. Implementing binding/semantic analysis support for a new C# language feature
      1. Getting Started
      2. How to do it...
      3. How it works...
      4. There's more...
    5. Implementing lowering/code generation support for a new C# language feature
      1. Getting Started
      2. How to do it...
      3. How it works...
    6. Writing unit tests for C# parsing, binding, and codegen phases
      1. Getting Started
      2. How to do it...
  11. Command-Line Tools Based on Roslyn API
    1. Introduction
    2. Writing an application based on the Compiler Syntax API to parse and transform source files
      1. Getting started
      2. How to do it...
      3. How it works...
    3. Writing an application based on the Compiler Semantic API to display diagnostics and overload resolution results
      1. Getting started
      2. How to do it...
      3. How it works...
    4. Writing an application based on the Compiler Analyzer API to execute diagnostic analyzers and display analyzer diagnostics
      1. Getting started
      2. How to do it...
      3. How it works...
    5. Writing an application based on the Workspaces API to format and simplify all source files in the solution
      1. Getting started
      2. How to do it...
      3. How it works...
    6. Writing an application based on the Workspaces API to edit projects in a solution and display project properties
      1. Getting started
      2. How to do it...
      3. How it works...