Can We Add More Than 1 Violation to a Single Element???

Nov 12, 2010 at 9:05 AM

Hello Everyone, 

I need some clarification on one point while writing custom StyleCop rule (Adding Violation), while doing a walk element i am capturing certain list, and on completion of all rule checking i want to mark violations for my captured list, When i am using AddViolation method, StyleCop tells me that only 1 Violation per Element. 

Is there any workaround, to Add Violation to the list generated?

Thanks N Regards,

Jackson C.

Nov 12, 2010 at 8:59 PM

An element may be associated with an infinite number of violations. You need to call AddViolation once per violation. If this isn't working for you, could you include a snippet of the your code?

 

Nov 16, 2010 at 9:49 AM

Hi Jason, 

Please check below code snippet. 

 

[SourceAnalyzer(typeof(CsParser))]

   public class CustomRules : SourceAnalyzer

   {

       ArrayList arrList1 = new ArrayList();

       public override void AnalyzeDocument(CodeDocument document)

       {

       //I am getting my arraylist from other functions.

arrList1 = getMyViolationsList(); 

document2.WalkDocument(new CodeWalkerElementVisitor<object>(this.VisitElementReportViolation), null, null);

     }

    }

 

private bool VisitElementReportViolation(CsElement element, CsElement parentElement, object context)

       {

           try

           {

               if ((arrList1.Count != 0) || (arrList1 != null))

               {

                                     for (int i = 0; i < finalArrList.Count; i++)

                   {

                       this.AddViolation(element, "LocalizationChecking", arrList1[i].ToString());

                   }

               }

               return true;

           }

           catch (Exception ex)

           {

               return false;

           }

       }

 

AddViolation method does not prints all violations, which are there in arraylist. instead Addviolation prints only first violation present in violation list. 

Is there any way to print all this violations which are present in this arrList.???

 

Thanks N Regards,

Jackson C.