Do you have hundreds and hundreds of StyleCop issues to fix in short time ?
Are you looking for ways to quickly/automatically Fix StyleCop issues in your C# code ?
Then in that case, you have two options:
- Use Resharper
- Resharper is a great tool for fixing such issues, but it comes with a licensing cost.
- Use Free extensions available in Visual Studio marketplace
Well there is one, available as a Visual Studio Extension, its called "Code Formatter".
This tool in combination with StyleCop works very efficiently in resolving most of the StyleCop issues in code.
Below are some of the features of this tool:
• Format the class file (.cs) to arrange members of class in the proper order.
• Using statements are brought inside namespace block.
• Unwanted namespaces are removed.
• Unwanted space are removed.
• Proper space introduced where ever required.
• Adds header comments on members of the class.
• Can group members by regions
Here are the simple steps that you can follow, to install and get started with this extension:
- Install StyleCop & Code Formatter Extensions from Visual Studio => Tools => Extension & Updates.
- Once installed, you can configure the Code Formatter tool from Tools => Options => Code Formatter => Update Settings
- You will see three tabs in the Code Formatter Settings Popup:
- Default - here you will see basic settings as to what all code you want to generate automatically
- Type Layout
- File Header - here you can specify the format of the header you want for your class files with auto generated code.
- Once you have set up all of these, now its time for the real action. Open any class file, where you want to fix StyleCop violations.
- In my case, i am using UserBUS.cs class file, which has around 111 StyleCop Violations (File Header Missing, No Summary Tag on class, methods, No Proper Indentation, etc) as shown below:
- Now we just need to right click on text editor, and from the context menu, select "Format Code" option.
- You will notice that it completes adding the auto generated code within seconds. Next all you need to do is to save the class file, and Run StyleCop again on the same file.
- And you will now see that all your 111 StyleCop Violations have vanished now, bringing the count to 0 StyleCop Violations.
There are ways to perform this code formatting in all .cs files present in your project, or solution. All you need to do is Right click on the Solution/Project node and go to "Code Formatter > Format Code" option on the menu.
When you do this, all the .cs files within the project or solution respectively will get formatted automatically.
Hope this is helpful. Cheers :)
PS: Code Formatter Extension manages to resolve most of the StyleCop violations, however there could be cases where you would have to do a fix manually like Method & variable naming, Using this qualifier, etc. But you will definitely end up saving a lot more amount of time that you would have spent on fixing most of these issues.
Great Post with valuable info. Thank you for the updates.
ReplyDeleteLearn Dot NET Online
Dot NET certification course online
Excellent Blog I like your blog and It is very informative. Thank you
ReplyDeleteRobot Framework Online Course
Selenium Online Training
Software Testing Online Course
I read this blog, a Nice article...Thanks for sharing and waiting for the next...
ReplyDeleteadvantages of r
features of r programming language
Really nice blog.keep surprising us
ReplyDeleteCoded UI Online Training
Excellant post keep sharing and worthy information.
ReplyDeleteAngularjs Training in Chennai
Angularjs Certification Online
Angularjs Training In Bangalore