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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:21:22+00:00 2026-05-12T19:21:22+00:00

In a class, I’ve got a trace method that looks like this: [System.Diagnostics.ConditionalAttribute(Trace)] private

  • 0

In a class, I’ve got a trace method that looks like this:

    [System.Diagnostics.ConditionalAttribute("Trace")]
    private void TraceOutput(TraceBits bits, string format, params object[] varParams)
    {
        if ((bits & _DesiredTrace) != 0)
        {
           ...emit trace here...
        }
    }

TraceBits is a [Flags] enum. Each call to TraceOutput passes in the bit that the call is tagged with. Like this:

    TraceOutput(TraceBits.Fill,
                "Fill     lock     wi({0}) stat({1}) iba({2}) nf({3})",
                workitem.index,
                workitem.status,
                workitem.inputBytesAvailable,
                _nextToFill
                );

The bits are: Create, Read, Write, Fill, etc. _DesiredTrace is a (private member variable) bitfield that indicates which trace statements should be actually emitted. This way I can selectively turn on trace statements for various portions of function within the class. If I want to trace only construction and destruction, I set the Create bit in that member bitfield.

I can use the ConditionalAttribute on the method, but that attribute doesn’t work on member variables or nested types (like TraceBits).

As a result, the types and variables that support tracing, are compiled into the code, whether or not Trace is defined. If Trace is undefined then these are completely unnecessary.

Is there a clean way to conditionally compile the supporting types and variables in?

I know I can use #if Trace ... #endif to surround the declaration and all invocations of TraceOutput, as well all the supporting stuff, but that uglifies the code. I like the cleaner look of the ConditionalAttribute, where each call to TraceOutput does not need to be bracketed with #if Trace.

What I really want is some way to use that attribute, or something like it, on nested classes and member variables. Is that possible?

  • 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-12T19:21:22+00:00Added an answer on May 12, 2026 at 7:21 pm

    Re the enum; the point about [Conditional] is that it is up to the caller‘s build whether to actually perform the invoke – the declaring/implementing code is always included; so assume that your code is a standalone library; you must include the enum, otherwise the caller (assume the caller is separate) cannot possibly ever call the method.

    It sounds like #if is a better fit for your problem – or you could use conditional inclusion in the build (csproj supports this, although it risks causing confusion; #if would be more obvious).

    Note that to perform overload resolution it must first identify the types, so you can’t really mix #if with [Conditional] since the enum is in the signature.

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

Sidebar

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I recently learned python with a strong C# background. My… May 13, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer I haven't personally used any Javascript BBcode parsers, but the… May 13, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer What you're describing isn't possible. In the case of your… May 13, 2026 at 12:33 am

Related Questions

I have text I am displaying in SIlverlight that is coming from a CMS
I have a French site that I want to parse, but am running into
In a class, I've got a trace method that looks like this: [System.Diagnostics.ConditionalAttribute(Trace)] private
In a class A I have two scopes, s1 and s2 which both join
With a class in Python, how do I define a function to print every

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.