Tutorial on writing stylecop custom rules (II)-add configuration parameters for validation rules

Source: Internet
Author: User

Previous tutorial writing stylecop custom rules tutorial (1) --- examples used in simple validation rules for writing Chinese remarks are to verify all attributes in the Code, without distinction between public and internal. This article uses the original example to add two rule attributes (includepublic and inculdeinternal). Based on the switch values of these two attributes, the rule controls whether to check whether the access permission in the Code is public or internal code.

 

Add the properties node to the XML file of the rule.

 

View Code <Properties>
<Booleanproperty
Name = "includepublic"
Defaultvalue = "true"
Friendlyname = "including pulbic"
Description = "Public attribute verification during code analysis"
/>
<Booleanproperty
Name = "includeinternal"
Defaultvalue = "true"
Friendlyname = "includes internal"
Description = "Check the internal attribute during code analysis"
/>
</Properties>

 

The csdocument class overload multiple upload document methods. In the previous tutorial, we used the upload document (codewalkerelementvisitor <Object> elementcallback, object context). The last parameter passed in a null value, in this article, you need to pass in the attribute verification switch value. Therefore, void upload document <t> (codewalkerelementvisitor <t> elementcallback, T context) is used to define a struct to pass in user configuration values. View code [structlayout (layoutkind. Sequential)]
Private struct analyzersettings
{
Public bool includepublic;
Public bool includeinternal;
}

Private analyzersettings loadsettings (codedocument document)
{
Analyzersettings result = new analyzersettings ();
Result. includeinternal = true;
Result. Required depublic = true;

If (document. settings! = NULL)
{
Booleanproperty addinsetting = Document. settings. getaddinsetting (this, "includepublic") as booleanproperty;
If (addinsetting! = NULL)
{
Result. includepublic = addinsetting. value;
}

Addinsetting = Document. settings. getaddinsetting (this, "includeinternal") as booleanproperty;
If (addinsetting! = NULL)
{
Result. includeinternal = addinsetting. value;
}
}

Return result;
}

 

The analyzedocument method that must be overwritten when the rule subclass is modified.

View code public override void analyzedocument (codedocument document)
{
Param. requirenotnull (document, "document ");
Csdocument Document2 = (csdocument) document;
If (document2.rootelement! = NULL )&&! Document2.rootelement. generated)
{
Analyzersettings settings = loadsettings (document );

Document2.initdocument <analyzersettings> (New codewalkerelementvisitor <analyzersettings> (this. checkdocumentationforelement), settings );
}
}

 

The last parameter of the original checkdocumentationforelement method is the input configuration value, which is verified based on the type and access permission of the Code node.

View code private bool checkdocumentationforelement (cselement element, cselement parentelement, analyzersettings settings)
{
If (base. Cancel)
{
Return false;
}

If (! Element. generated)
{
If (element. elementtype = elementtype. Property )&&
(Settings. includepublic & element. accessmodifier = accessmodifiertype. Public) |
(Settings. includeinternal & element. accessmodifier = accessmodifiertype. Internal )))
{
This. parseheader (element, element. header, element. linenumber, false );
}
}
Return true;
}

 

After compilation, place the DLL in the stylecop directory. Select the new rule in stylecop settings. You can see the two switch values of the rule in detailed settings on the right.

 

Code:/files/byeah/withsettingrule.zip

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.