title: Visual Studio Code analysis - Creating a new rule to count number of lines in methods
body: <p><strong>UPDATE</strong></p>
  
  <p><strong>I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am facing some problem which I put <a href="http://social.msdn.microsoft.com/Forums/en-US/vstscode/thread/70a7a2d8-9246-4d49-b504-3fec14b8b0d7/?prof=required&amp;lc=1033" rel="nofollow noreferrer">here</a> with all the details. Source code is <a href="http://4lyjtq.blu.livefilestore.com/y1ptWgIweqUKSdgxADEv_HqLiSW3IlAUNNfKo_Kc-Gx-3-nNhQ_m1c18jFflS7HICCmC8htbjFOs4Tj25whAJPHDrjak-fym9mX/EnforceLOCLimitRule.zip?download&amp;psid=1" rel="nofollow noreferrer">here</a>. I didn't want to increase the length of this question by putting all the details.</strong></p>
  
  <p>I am trying to write a code analysis rule which would raise warnings for methods having 
  more than 100 lines. I am following <a href="http://blogs.msdn.com/b/codeanalysis/archive/2010/03/26/how-to-write-custom-static-code-analysis-rules-and-integrate-them-into-visual-studio-2010.aspx" rel="nofollow noreferrer">this</a> article. However, I am unable to count the number of lines by following the API provided by  CodeAnalysis. for example,</p>
  
  <pre><code>public override ProblemCollection Check(Member member)
          {
              Method method = member as Method;
              if (method == null)
              {
                  return null;
              }
              CheckForLOC(method);
              return Problems;
          }
  </code></pre>
  
  <p>Following is the CheckForLOC()</p>
  
  <pre><code>private void CheckForLOC(Method method)
      {
          int startLineForMethod = method.Body.SourceContext.StartLine;
          int endLineForMethod = method.Body.SourceContext.EndLine;
          if (endLineForMethod &gt; startLineForMethod
              &amp;&amp; ((endLineForMethod - startLineForMethod) &gt; constMaximumLOCforAMethod))
          {
              Resolution resolution = GetResolution(method, constMaximumLOCforAMethod);
              Problem problem = new Problem(resolution);
              Problems.Add(problem);
          }
      }
  </code></pre>
  
  <p>In the above code, method.Body.SourceContext.StartLine and method.Body.SourceContext.EndLine return the same value. Not sure why. </p>
  
  <p>I also tried using the StatementCollection :-</p>
  
  <pre><code>private void CheckForLOC(Method method)
          {
              int LOCPerMethod = 0;
  
              if (method.Body.Statements.Count &gt;= 1)
              {
                  foreach (var statement in method.Body.Statements)
                  {
                      LOCPerMethod += GetNumberOfLinesPerStatement(statement);
                  }
  
              }
              if (LOCPerMethod &gt; constMaximumLOCforAMethod)
              {
                  Resolution resolution = GetResolution(method, constMaximumLOCforAMethod);
                  Problem problem = new Problem(resolution);
                  Problems.Add(problem);
              }
          }
  
          private int GetNumberOfLinesPerStatement(Statement statement)
          {
              int LOCperStatement = 0;
              if (statement.SourceContext.EndLine &gt; statement.SourceContext.StartLine)
              {
                  LOCperStatement = statement.SourceContext.EndLine - statement.SourceContext.StartLine;
              }
              return LOCperStatement;
          }
  </code></pre>
  
  <p>Here also, Statement.SourceContext.StartLine and Statement.SourceContext.EndLine return the same value. I see that the StartLine for each statement is different and one needs to substract the StartLine value of the one statement from its previous one's. However, I see that result is erratic. For example, in the below snippet in a method, It gives me the line number of Statement1 as StartLineNumber whereas It should give the StartLineNumber of If(SomeCondition):-</p>
  
  <pre><code>if(SomeCondition)
  {
     Statement1
     Statement2
     Statement3
  }
  </code></pre>
  
  <p>Could anybody provide some direction on this?</p>