This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

Microsoft.CodeAnalysis.BannedApiAnalyzers – DEV Community


Usually a occupation workforce of builders may have guidelines for coding together with code opinions to make sure code is writen to the foundations and pointers of the workforce.

Even with code opinions guidelines can nonetheless be damaged which brings us to a Roslyn Analyzer Microsoft.CodeAnalysis.BannedApiAnalyzers MCABAA NuGet package deal which gives a solution to carry out a pre-check on a code evaluate and/or whereas writing code.



The best way to use MCABAA

  • Add the package to a mission
  • Create a textual content file named BannedSymbols.txt within the root folder of the mission.

Examine the mission file by double clicking the mission file in Resolution Explorer and observe how the banned file was positioned in.

  <ItemGroup>
      <None Take away="BannedSymbols.txt" />
  </ItemGroup>

  <ItemGroup>
      <AdditionalFiles Embody="BannedSymbols.txt" />
  </ItemGroup>
Enter fullscreen mode

Exit fullscreen mode



Including guidelines



Instance 1

Suppose for working with SQL-Server database with a knowledge supplier the package deal Microsoft.Information.SqlClient shouldn’t be used however as a substitute Entity Framework Core. Add the next rule (as the primary name is often creating an occasion of SqlConnection.

T:Microsoft.Information.SqlClient.SqlConnection; Use EF Core as a substitute
Enter fullscreen mode

Exit fullscreen mode

One other rule is required within the occasion a developer makes an attempt utilizing System.Information.SqlClient

T:System.Information.SqlClient.SqlConnection; Use EF Core as a substitute
Enter fullscreen mode

Exit fullscreen mode

On the identical line, stop a neighborhood technique from getting used.

M:Library1.Lessons.DataOperations.ReadData; use EF Core as a substitute
Enter fullscreen mode

Exit fullscreen mode



Instance 2

Disallow utilizing Newtonsoft.Json.JsonConvert, use System.Textual content.Json.JsonSerializer as a substitute.

T:Newtonsoft.Json.JsonConvert;Use System.Textual content.Json.JsonSerializer as a substitute
Enter fullscreen mode

Exit fullscreen mode



Instance 3

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

P:System.DateTime.Now;Use System.DateTime.UtcNow as a substitute
Enter fullscreen mode

Exit fullscreen mode



Instance 4

The workforce has two variations of a category, on this case Individual in two separate class initiatives.

Library1 class mission Individual class

public class Individual
{
    public int Id { get; set; }
    public string Kind { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }
}
Enter fullscreen mode

Exit fullscreen mode

Library2 class mission Individual class

public class Individual
{
    public int Id { get; set; }
    public string Kind { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly? BirthDate { get; set; }
}
Enter fullscreen mode

Exit fullscreen mode

Library1 is for pre-existing mission so it want to remain however for brand spanking new initiatives, use Library2. We add the next.

T:Library1.Fashions.Individual;Use Library2.Fashions.Individual which makes use of DateOnly somewhat than DateTime.
Enter fullscreen mode

Exit fullscreen mode



Discovering damaged guidelines.

Construct the mission(s) and verify the Error Record in Visible Studio.

Error List window in Visual Studio

What the code seems like with damaged guidelines

Broken rules in the code editor



Guidelines cheat sheet

See the next page



Caveats

  • The file BannedSymbols.txt could be positioned in a folder exterior the mission however might not all the time work, its hit or miss.
  • Guidelines cannot be utilized to language extension strategies.



Supply code

Reasonably than taking time to setup a mission, clone the next GitHub repository.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?