Two Classes in File

Feb 4, 2012 at 12:09 PM


I want to proove if there are two classes in one File. Therefor I count the number of classes with this Method. 

private void CheckNumberOfClasses(CsDocument csdocument)


CsElement root = csdocument.RootElement;

int counter=0;

foreach (CsElement element in root.ChildElements)


if (element.ElementType.Equals(ElementType.Class))



if (counter > 1)


this.AddViolation(element, "TwoClassesInFile");




StyleCop shows a violation for each declaration of an class, when I repeat the analysis.

Thanks for help. 

Feb 6, 2012 at 5:17 PM

I don't understand where is the problem, but the rule your trying tio write already exists in stylecop

It's rule "SA1402 : FileMayOnlyContainASingleClass"

It's in "Maintainability Rules / File Contents"