Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以在运行时在C#中检查编译器常量?_C#_.net_Compiler Construction_Compiler Constants - Fatal编程技术网

是否可以在运行时在C#中检查编译器常量?

是否可以在运行时在C#中检查编译器常量?,c#,.net,compiler-construction,compiler-constants,C#,.net,Compiler Construction,Compiler Constants,我知道我可以通过有条件地包含如下代码来有条件地设置变量: #if DEBUG someVar = "foo"; #else someVar = "bar"; #endif 我宁愿在运行时枚举或测试编译器常量 例如,我想将编译时定义的所有符号放在一个窗口标题中,以便测试人员可以看到他们正在测试的版本。我认为您无法动态枚举它们 在C#中使用编译器常量所能做的唯一事情就是定义它们并用def,unde取消定义它们,然后查看它们是否与if,else,elif,endif一起存在,一旦程序被编

我知道我可以通过有条件地包含如下代码来有条件地设置变量:

#if DEBUG
   someVar = "foo";
#else
   someVar = "bar";
#endif
我宁愿在运行时枚举或测试编译器常量

例如,我想将编译时定义的所有符号放在一个窗口标题中,以便测试人员可以看到他们正在测试的版本。

我认为您无法动态枚举它们 在C#中使用编译器常量所能做的唯一事情就是定义它们并用
def
unde
取消定义它们,然后查看它们是否与
if
else
elif
endif
一起存在,一旦程序被编译,就没有办法知道你有什么变量,除非你这样做

private static List<string> compileConstants = new List<string>();

#if DEBUG
  compileConstants.Add("DEBUG");
#endif
private static List compileConstants=new List();
#如果调试
compileConstants.Add(“调试”);
#恩迪夫

除此之外,我认为没有其他办法。编译器常量是在将信息传递给程序集时定义和使用的。我不认为它实际上是以一种您可以在C#

中的运行时访问它们的方式持久化的。我认为预处理器会在编译时删除未使用的代码

但您可以使用以下方法以更干净的方式完成相同的任务:


然后,只需正常调用该方法,如果定义了DEBUG,则会更改窗体的标题;如果未定义DEBUG,则该方法调用在代码中仍然出现,则不会执行任何操作。

编译器常量不会存储在已编译程序集中的任何位置。因此,无法在运行时访问它们

它们也不会存储在调试数据库(.pdb文件)中。
[Conditional("DEBUG")]
public void DrawDebugTitle() {
   Form1.Title = "Debug mode";
}