【问题标题】:C# #if / #ifdef syntax doesn't compile, why?C# #if / #ifdef 语法无法编译,为什么?
【发布时间】:2010-08-25 15:27:34
【问题描述】:

为什么下面的代码不能编译(sn-p)?

  public enum ApplicationType : int
  {
   CONSOLE = 1,
   WINDOWS_FORMS = 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

【问题讨论】:

    标签: c# .net syntax conditional-compilation


    【解决方案1】:

    你遇到了什么错误?

    无论如何,( m_iApplicationType == ApplicationType.ASP_NET ) 不是编译时间常数。

    【讨论】:

    【解决方案2】:

    您将#if 与成员变量一起使用是无效的。它仅对您使用 #define 指令创建的符号进行操作,如下所示:

    #define ASP_NET
    
    #if(ASP_NET)
    // put your conditional compilation code here
    #endif
    
    #if(CONSOLE)
    // your console-related code goes here
    #endif
    

    在这种情况下,将只编译 #if(ASP_NET) 块中的代码,因为未定义 CONSOLE

    【讨论】:

    • 预编译器符号(它们不获取值,更不用说变化的值,因此不是真正的变量)也可以在编译器命令行上设置(包括通过 MSBuild 脚本或 VS Project 设置)。
    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 2014-08-02
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多