"Conditional" attribute confusion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marek

    "Conditional" attribute confusion

    I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
    Here is the code that illustrates th eproblem:

    [Conditional("DE BUG")]
    public static void MethodA()
    {
    MethodB()
    }

    #if DEBUG
    public static void MethodB()
    {
    ....
    }
    #endif

    my expectation was that this code will compile perfectly when DEBUG
    condition is defined as well as when it is undefined. Apparently, that is
    not the case. When compiling with "DEBUG" undefined, compiler generates
    error: "The name MethodB does not exist in current context."

    This is what Microsoft help says about ConditionalAttr ibute class:
    "Indicates to compilers that a method call or attribute should be ignored
    unless a specified conditional compilation symbol is defined". The
    conclusion from this is that MethodA from my example should not be compiled.
    The reality is that the nethod is compiled - otherwise the error would not
    have been reported.

    Is there something I am missing about "Conditiona l" attribute and how the
    compiler is working or it something .NET framework documentation or/and C#
    compiler are missing ?

    Thank you.


  • Marek

    #2
    Re: "Condition al" attribute confusion

    Well, 2 minutes after I submitted this post I found the answer to my
    question. Certainly it was me missing the point. "Conditiona l" attribute
    informs compiler to ignore the call to the method but not to ignore the
    method during the compilation.

    My apology for posting something that I should not have.




    >I use VS2005 with framework 2.0 and I just found a behavior I consider odd.
    >Here is the code that illustrates th eproblem:
    >
    [Conditional("DE BUG")]
    public static void MethodA()
    {
    MethodB()
    }
    >
    #if DEBUG
    public static void MethodB()
    {
    ...
    }
    #endif
    >
    my expectation was that this code will compile perfectly when DEBUG
    condition is defined as well as when it is undefined. Apparently, that is
    not the case. When compiling with "DEBUG" undefined, compiler generates
    error: "The name MethodB does not exist in current context."
    >
    This is what Microsoft help says about ConditionalAttr ibute class:
    "Indicates to compilers that a method call or attribute should be ignored
    unless a specified conditional compilation symbol is defined". The
    conclusion from this is that MethodA from my example should not be
    compiled. The reality is that the nethod is compiled - otherwise the
    error would not have been reported.
    >
    Is there something I am missing about "Conditiona l" attribute and how the
    compiler is working or it something .NET framework documentation or/and C#
    compiler are missing ?
    >
    Thank you.
    >
    >

    Comment

    Working...