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 8786601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:40:39+00:00 2026-06-13T21:40:39+00:00

I have the following method: [ExcludeFromCodeCoverage] private static string GetAuthorizationToken(HttpActionContext actionContext) { var authorization

  • 0

I have the following method:

[ExcludeFromCodeCoverage]
private static string GetAuthorizationToken(HttpActionContext actionContext)
{
    var authorization = actionContext.Request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
    // ... removed for brevity
}

The ExcludeFromCodeCoverage attribute works for the whole method, except for h.Key.Equals("Authorization"), which shows as uncovered despite the attribute on the method.

How can I exclude this symbol from code coverage results?

  • 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-06-13T21:40:40+00:00Added an answer on June 13, 2026 at 9:40 pm

    I can explain why it shows as not being covered even though you’ve specified the ExcludeFromCodeCoverage attribute on the class. The explanation lies in the IL that is actually generated by the compiler. It declares a delegate to match the lambda expression which looks something like this.

    private static Func<string, bool> AnonymousMethodDelegate;
    

    It then creates the named method that matches the delegate declaration.

    [CompilerGenerated]
    private static bool CompilerGeneratedMethod(string h)
    {
      return h.Equals("Authorization");
    }
    

    Then finally is calls into the named method using the delegate in your method something like this

    [ExcludeFromCodeCoverage]
    private static string GetAuthorizationToken(HttpActionContext actionContext)
    {
      AnonymousMethodDelegate = CompilerGeneratedMethod;
      Func<string, bool> predicate = AnonymousMethodDelegate;
      return Enumerable.FirstOrDefault<string>((IEnumerable<string>) actionContext, predicate);
    }
    

    I’m sure you’ve spotted the problem. The compiler has created a method which no longer has your ExcludeFromCodeCoverage attribute attached! This is the reason that NCover judges it as having not been covered even though you appear to have declared the ExcludeFromCodeCoverage attribute.

    One way to exclude it would be to go back to basics and effectively do what the compiler is doing for you and declare the delegate and named method. This gives you the power to add the ExcludeFromCodeCoverage attribute to that named method. I’m not a big fan of this solution though for obvious reasons.

    Obviously if you add the ExcludeFromCodeCoverage attribute at the class level it will also cover this generated method which would get around this problem but I assume this would also exclude code that you wanted to include in the coverage report.

    The other alternative is that if you’re using NCover then you could use the fact that all the compiler generated methods are decorated with the CompilerGenerated attribute. You can pass attributes to be excluded to NCover so all you would have to do is pass System.Runtime.CompilerServices.CompilerGeneratedAttribute either on the command line or via the MSBuild task. This is probably my preferred option.

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

Sidebar

Related Questions

I have the following method in some nemerle code: private static getLinks(text : string)
I have a following method private void SetProcessDocumentStatus(string status) { var setStatusWith = new
I have the following method: public static T ExecuteScalar<T>( string query, SqlConnection connection, params
I have the following method in an external class public static void DoStuffWithAnimals(IDictionary<string, Animal>
I have following simple method in my main activity. private void showMap(String name )
I have the following method: private byte[] GetEmailAsBytes(string lstrBody) { byte[] lbytBody; ASCIIEncoding lASCIIEncoding
I have the following method to sum time: public static String sumTime(String date1, String
I have the following method: public static string PrepareNumberForInserting(string pNumber) { if (pNumber.Length >
I have the following method: public string Phase(string phase) { return Phase 1; }
I have the following method: [HttpPost] [AjaxOnly] public JsonResult ValidateInput(string text) { return new

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.