Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1061205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:26:35+00:00 2026-05-16T18:26:35+00:00

UPDATE I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am

  • 0

UPDATE

I reflected Microsoft.Cci.dll and build my rule. It works fine. However, I am facing some problem which I put here with all the details. Source code is here. I didn’t want to increase the length of this question by putting all the details.

I am trying to write a code analysis rule which would raise warnings for methods having
more than 100 lines. I am following this article. However, I am unable to count the number of lines by following the API provided by CodeAnalysis. for example,

public override ProblemCollection Check(Member member)
        {
            Method method = member as Method;
            if (method == null)
            {
                return null;
            }
            CheckForLOC(method);
            return Problems;
        }

Following is the CheckForLOC()

private void CheckForLOC(Method method)
    {
        int startLineForMethod = method.Body.SourceContext.StartLine;
        int endLineForMethod = method.Body.SourceContext.EndLine;
        if (endLineForMethod > startLineForMethod
            && ((endLineForMethod - startLineForMethod) > constMaximumLOCforAMethod))
        {
            Resolution resolution = GetResolution(method, constMaximumLOCforAMethod);
            Problem problem = new Problem(resolution);
            Problems.Add(problem);
        }
    }

In the above code, method.Body.SourceContext.StartLine and method.Body.SourceContext.EndLine return the same value. Not sure why.

I also tried using the StatementCollection :-

private void CheckForLOC(Method method)
        {
            int LOCPerMethod = 0;

            if (method.Body.Statements.Count >= 1)
            {
                foreach (var statement in method.Body.Statements)
                {
                    LOCPerMethod += GetNumberOfLinesPerStatement(statement);
                }

            }
            if (LOCPerMethod > 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 > statement.SourceContext.StartLine)
            {
                LOCperStatement = statement.SourceContext.EndLine - statement.SourceContext.StartLine;
            }
            return LOCperStatement;
        }

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):-

if(SomeCondition)
{
   Statement1
   Statement2
   Statement3
}

Could anybody provide some direction on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:26:35+00:00Added an answer on May 16, 2026 at 6:26 pm

    This is more of a style rule than a correctness rule, so it would be a better candidate for a StyleCop rule than an FxCop rule.

    That said, if you really want to implement it via FxCop, you should take a look at how the Microsoft.FxCop.Sdk.MethodMetrics.CalculateLinesOfCode(Method) accomplishes the same task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[Update]: my initial example doesn't reflect my problem. Updated the sample, hopfully it is
Update : This is no longer an issue from C# 6, which has introduced
I'm customizing a .csproj project to run some custom tasks before the main build.
Update: The problem below occurs when I add: <script src=http://code.jquery.com/jquery-1.7.1.min.js></script> I'm trying to redirect
I've set up a resource in routes.rb : map.resource :papers which is reflected in
Problem UPDATE : This issue, it turns out, has little to do with the
I'm changing a .py file which runs part of a Twisted environment. However the
I want to Delete a row in the Gridview which is in the update
I have an IQueryable object and I want to update some values manually, but
I have a Silverlight Listbox bound to Observablecollection which shows up fine (first time)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.