zoukankan      html  css  js  c++  java
  • C# 预定义语言

    官网:

    https://msdn.microsoft.com/zh-cn/library/88td0y52.aspx

    [Conditional("DEBUG")]

    作为预处理中的一对:#region name ,#endregion可能是大家使用得最多的,我也常用它来进行代码分块,在一个比较长的cs文件中,这么做确实是一件可以让你使代码更清晰的好办法,VS也自动用这个来包含自动生成的代码,它这么做既可以使开发人员更清晰的查看自己的代码,也开了一个好头,使更多人使用#region。 
    C#中还有好几对预处理指令,可能大家就用得比较少了。 
    #define symbol 
    #undef symbol 
    #if symbol [operator symbol2]... 
    #else 
    #elif symbol [operator symbol2] 
    #endif 
    #warning text text指在编译器输出中的警告文字 
    #error text  text指在编译器输出中的错误信息 
    #line number [file]

    关于#define要说明几点,#define DEBUG 等效于不写这句话,你要不定义它才一定要写上#undefin DEBUG 
     (C# Essentials一书中说 #define DEBUG 等效于#define DEBUG true,不过这个好像有问题)

    这一段代码可以说明好几个命令: 
    #define DEBUG 
    #define VC_V6 
    using System; 
    public class MyClass 

       public static void Main() 
       {

          #if (DEBUG && !VC_V6) 
             Console.WriteLine("DEBUG is defined"); 
          #elif (!DEBUG && VC_V6) 
             Console.WriteLine("VC_V6 is defined"); 
          #elif (DEBUG && VC_V6) 
             Console.WriteLine("DEBUG and VC_V6 are defined"); 
          #else 
             Console.WriteLine("DEBUG and VC_V6 are not defined"); 
          #endif 
       } 

    我们可以用这些指令来自动处理测试期和发布期数据库的连接字符串之类的工作 
    #if !RELEASE 
    constr = testSQLServerConnectionString 
    #else 
    constr = releaseSQLServerConnectionString 
    #endif 
    在编代码时加上一句#undef RESEASE,发布后删除这一句就行了。


    #define DEBUG

    using System;

    namespace SyntaxTest 

     public class Class2 
     { 
      public Class2() 
      { 
       #if DEBUG 
              #warning DEBUG is defined 
          #endif 
      } 
     } 

    以上代码会在编译时产生一个警告。


    #define DEBUG

    using System;

    namespace SyntaxTest 

     public class Class2 
     { 
      public Class2() 
      { 
       #if DEBUG 
              #error DEBUG is defined 
          #endif 
      } 
     } 

    以上代码会在编译时产生一个错误,编译不通过,所以我们可以利用以上两种指令进行一些自定义的编译检测。 

    #line 使您得以修改编译器的行号以及(可选)错误和警告的文件名输出。

    #line [ number ["file_name"] | default ]

    number 
    要为源代码文件中后面的行指定的编号。 
    "file_name"(可选) 
    希望出现在编译器输出中的文件名。默认情况下,使用源代码文件的实际名称。文件名必须括在双引号 ("") 中。 
    default 
    重置文件中的行编号。 
    备注 
    #line 可能由生成过程中的自动中间步骤使用。例如,如果中间步骤从原始的源代码文件中移除行,但是您仍希望编译器基于文件中的原始行号生成输出,则可以移除行,然后用 #line 模拟原始行号。


    public class MyClass2 

       public static void Main() 
       { 
          #line 200 
          int i;   // 这一行在200行,不过在VS中看到的还是6 
          #line default 
          char c;   // 这一行在7行,不过在VS中看到的还是8 
       } 
    }

      

  • 相关阅读:
    ul不加宽高
    获取元素的外部样式问题
    设置定时器、重启定时器要注意的问题
    php的var关键字
    抽象类(abstract class)和 接口(interface)
    __sleep和__wakeup
    类型约束
    TensorFlow 拾遗
    Datasets and Evaluation Metrics used in Recommendation System
    触龙——可解释推荐系统
  • 原文地址:https://www.cnblogs.com/lizhenlin/p/6574083.html
Copyright © 2011-2022 走看看