Microsoft.CodeAnalysis.BannedApiAnalyzers

Typically a profession team of developers will have rules for coding along with code reviews to ensure code is writen to the rules and guidelines of the team.

Even with code reviews rules can still be broken which brings us to a Roslyn Analyzer Micros…


This content originally appeared on DEV Community and was authored by Karen Payne

Typically a profession team of developers will have rules for coding along with code reviews to ensure code is writen to the rules and guidelines of the team.

Even with code reviews rules can still be broken which brings us to a Roslyn Analyzer Microsoft.CodeAnalysis.BannedApiAnalyzers MCABAA NuGet package which provides a way to perform a pre-check on a code review and/or while writing code.

How to use MCABAA

  • Add the package to a project
  • Create a text file named BannedSymbols.txt in the root folder of the project.

Inspect the project file by double clicking the project file in Solution Explorer and note how the banned file was placed in.

  <ItemGroup>
      <None Remove="BannedSymbols.txt" />
  </ItemGroup>

  <ItemGroup>
      <AdditionalFiles Include="BannedSymbols.txt" />
  </ItemGroup>

Adding rules

Example 1

Suppose for working with SQL-Server database with a data provider the package Microsoft.Data.SqlClient should not be used but instead Entity Framework Core. Add the following rule (as the first call is usually creating an instance of SqlConnection.

T:Microsoft.Data.SqlClient.SqlConnection; Use EF Core instead

Another rule is needed in the event a developer attempts using System.Data.SqlClient

T:System.Data.SqlClient.SqlConnection; Use EF Core instead

On the same line, prevent a local method from being used.

M:Library1.Classes.DataOperations.ReadData; use EF Core instead

Example 2

Disallow using Newtonsoft.Json.JsonConvert, use System.Text.Json.JsonSerializer instead.

T:Newtonsoft.Json.JsonConvert;Use System.Text.Json.JsonSerializer instead

Example 3

Disallow System.DateTime.Now in favor of Use System.DateTime.UtcNow

P:System.DateTime.Now;Use System.DateTime.UtcNow instead

Example 4

The team has two versions of a class, in this case Person in two separate class projects.

Library1 class project Person class

public class Person
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }
}

Library2 class project Person class

public class Person
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly? BirthDate { get; set; }
}

Library1 is for pre-existing project so it need to stay but for new projects, use Library2. We add the following.

T:Library1.Models.Person;Use Library2.Models.Person which uses DateOnly rather than DateTime.

Finding broken rules.

Build the project(s) and check the Error List in Visual Studio.

Error List window in Visual Studio

What the code appears like with broken rules

Broken rules in the code editor

Rules cheat sheet

See the following page

Caveats

  • The file BannedSymbols.txt can be placed in a folder outside the project but may not always work, its hit and miss.
  • Rules can not be applied to language extension methods.

Source code

Rather than taking time to setup a project, clone the following GitHub repository.


This content originally appeared on DEV Community and was authored by Karen Payne


Print Share Comment Cite Upload Translate Updates
APA

Karen Payne | Sciencx (2023-03-19T19:15:10+00:00) Microsoft.CodeAnalysis.BannedApiAnalyzers. Retrieved from https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/

MLA
" » Microsoft.CodeAnalysis.BannedApiAnalyzers." Karen Payne | Sciencx - Sunday March 19, 2023, https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/
HARVARD
Karen Payne | Sciencx Sunday March 19, 2023 » Microsoft.CodeAnalysis.BannedApiAnalyzers., viewed ,<https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/>
VANCOUVER
Karen Payne | Sciencx - » Microsoft.CodeAnalysis.BannedApiAnalyzers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/
CHICAGO
" » Microsoft.CodeAnalysis.BannedApiAnalyzers." Karen Payne | Sciencx - Accessed . https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/
IEEE
" » Microsoft.CodeAnalysis.BannedApiAnalyzers." Karen Payne | Sciencx [Online]. Available: https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/. [Accessed: ]
rf:citation
» Microsoft.CodeAnalysis.BannedApiAnalyzers | Karen Payne | Sciencx | https://www.scien.cx/2023/03/19/microsoft-codeanalysis-bannedapianalyzers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.