FXCOP First Experience

Source: Internet
Author: User

Code quality is important to the success or failure of a software project, which I think we all understand. So how do you ensure code quality in a software team? For this problem different people may have different answers, for me I feel that doing a good job of two code quality basically can guarantee:

1. Code specification (specific specifications can refer to the. NET Design Specification book)

2. Testing (mainly unit tests for programmers themselves)

Microsoft provides foxcop to check code specifications

Nunit to complete unit testing

For NUnit This blog has an introductory article, and now to introduce FXCOP (due to personal preferences, I do not like some tools such as SVN, NUnit, and VS integration, this article does not play FxCop and VS integration, but many of these online articles, the need for readers to search by themselves)

First, the installation of FXCOP software

You can get FxCop's installation package from various sources, I also provide FxCop1.36 download here

Follow the prompts to install after download, install and start FxCop

  

Second, check whether the assembly conforms to code specification

Project--Add Targets--and Analyze as shown (in the Rules Tab we can select the enabled rule)

Iii. Custom Rules

1. New Class Library entry added FxCopSdk.dll and Microsoft.Cci.dll under the FxCop 1.36 installation directory

2. Create a new rule XML, as shown below, and compile it as a resource

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <RulesFriendlyName= "Xxxsoft company naming Specification">3     <RuleTypeName= "Xxxsoftnamerulemethod"Category= "Xxxsoft.namerule"Checkid= "XX0001">4         <Name>Method Name Rule</Name>5         <Description>The method name must be capitalized, and a warning will be prompted if the first character is lowercase.</Description>6         <URL/>7         <Resolution>The first letter of method ' {0} ' should be uppercase</Resolution>8         <MessagelevelCertainty= " the">Error</Messagelevel>9         <Email>[Email protected]</Email>Ten         <fixcategories>nonbreaking, Dependsonfix</fixcategories> One         <Owner>Servicesoft</Owner> A     </Rule> -  -     <RuleTypeName= "Xxxsoftnamerulefield"Category= "Xxxsoft.namerule"Checkid= "XX0002"> the         <Name>Field name rule</Name> -         <Description>The access modifier for a field cannot be public.</Description> -         <URL/> -         <Resolution>The Access modifier for field ' {0} ' is public</Resolution> +         <MessagelevelCertainty= " the">Error</Messagelevel> -         <Email>[Email protected]</Email> +         <fixcategories>nonbreaking, Dependsonfix</fixcategories> A         <Owner>Servicesoft</Owner> at     </Rule> - </Rules>
View Code

3. Write Rule class

1 using MICROSOFT.FXCOP.SDK;2 3 namespace Xxxcompanyrules4 {5 Public class Fieldrule1:baseintrospectionrule6     {7 Public FieldRule1 ():8 Base ("Xxxsoftnamerulefield", "Xxxcompanyrules.rules", typeof (FieldRule1). Assembly)9         {}Ten  One Public override Problemcollection Check (Member Member) A         { - if (member. DeclaringType is Enumnode) -             { the return null; -             } - Field field = member as Field; - if (field = = null) +             { - return null; +             } A if (field. IsPublic) at             { - Problems.add (New Problem (Getresolution (member. (name.name) )); -             } - return problems; -         } -     } in}
View Code
1 usingSystem;2 usingMICROSOFT.FXCOP.SDK;3 4 namespaceXxxcompanyrules5 {6      Public classMethodrule1:baseintrospectionrule7     {8          PublicMethodRule1 ():9             Base("Xxxsoftnamerulemethod","Xxxcompanyrules.rules",typeof(MethodRule1). Assembly)Ten         { } One  A         /// <summary> -         ///Verify first Letter -         /// </summary> the         /// <param name= "Member" >type member</param> -         /// <returns>Error Hints</returns> -          Public Overrideproblemcollection Check (Member Member) -         { +METHOD = Member asMethod; -             if(Method = =NULL) +             { A                 return NULL; at             } -             if(!method. Isspecialname &&! Char.isupper (method. Name.name,0)) -             { -Problems.add (Newproblem (Getresolution (member. (name.name) )); -             } -             returnproblems; in         } -     } to}
View Code

4. Compiling the build rule DLL

5. Add to FxCop

6. Verification

This section of the code download

End of this article

FXCOP First Experience

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.