C# #if / #ifdef syntax doesn't compile, why?
Why does the below code not compile (snippet) ?
public enum ApplicationType : int
{
CONSOLE = 1,
WINDOWS_FORMS = 开发者_StackOverflow中文版2,
ASP_NET = 3,
WINDOWS_SERVICE = 4,
MUTE = 5
}
//#if( false)
//#if (DEBUG && !VC_V7)
#if( m_iApplicationType != ApplicationType.ASP_NET )
public class HttpContext
{
public class Current
{
public class Response
{
public static void Write(ref string str)
{
Console.WriteLine(str);
}
}
}
}
#endif
What error are you getting?
In any case, ( m_iApplicationType == ApplicationType.ASP_NET )
is not a compile time constant.
Your use of #if
with a member variable is invalid. It operates only on symbols that you create with the #define
directive, like so:
#define ASP_NET
#if(ASP_NET)
// put your conditional compilation code here
#endif
#if(CONSOLE)
// your console-related code goes here
#endif
In this case, only the code within the #if(ASP_NET)
block will be compiled because CONSOLE
is not defined.
精彩评论