How to do it...

  1. Start off by creating a new console application. You can call it whatever you like. In my example, I simply called it DiagAnalyzerDemo.
  1. From the Tools menu, select NuGet Package Manager and then Manage NuGet Packages for Solution....
  1. In the Browse tab, search for Code-Cracker. The results should return the codecracker.CSharp NuGet package. Check the project you want to apply the NuGet package to and click on the Install button.
  1. Visual Studio will allow you to review the changes that are about to be made. Click on the OK button to continue.
  1. Click on I Accept when the license terms are displayed.
  1. When the NuGet package is installed, the results will be displayed in the Output window.
  1. Looking at your project you will notice that the CodeCracker.CSharp analyzer has been added under the Analyzers node in your Solution Explorer.
  1. If you expand the CodeCracker.CSharp analyzer, you will see all the individual analyzers included in the NuGet package.
  1. There is however a better place to review these analyzers from. From the Project menu, go to the [project name] properties menu item. In my case this is DiagAnalyzerDemo Properties....
  1. Click on the Open button to open the rule set.
  1. Here you will see a collection of all the analyzers available; from this screen, you can modify the action of specific analyzers.
  1. In your code, add the following class. You can call it whatever you like, but for simplicity's sake use the following example. You will see that I have a constructor that sets a property called DimensionWHL. This property just returns an array with the width, height, and length values. Not very nice code indeed.
        public class ShippingContainer
{
public int Width { get; set; }
public int Height { get; set; }
public int Length { get; set; }
public int[] DimensionsWHL { get; set; }
public ShippingContainer(int width, int height, int length)
{
Width = width;
Height = height;
Length = length;

DimensionsWHL = new int[] { width, height, length };
}
}
  1. Return back to the analyzers screen and search for the word properties. You will see an analyzer returned called CA1819, which specifies that a property should never return an array. The Action is changed to Warning, but you can change this to Error should you wish by clicking on the word Warning under the Action column and selecting Error.
  1. Save the changes and go and build your console application. You will see that the warning for the code analyzer CA1819 is displayed in the Error List. If you had changed the action to Error, the build would have broken with that error.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.133.160