#define PI
#define DEBUG_MODE
#define VC_V10
#define TEMP_SYMBOL
#undef TEMP_SYMBOL
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace CSharpPreprocessorLab
{
internal class Program
{
#pragma warning disable 414
private int unusedDiagnosticField = 42;
#pragma warning restore 414
#region ConditionalHelpers
private static string EvaluateDefineAndUndef()
{
string piStatus;
#if (PI)
piStatus = "PI is defined";
#else
piStatus = "PI is not defined";
#endif
string tempStatus;
#if TEMP_SYMBOL
tempStatus = "TEMP_DEFINED";
#else
tempStatus = "TEMP_UNDEF";
#endif
return piStatus + "|" + tempStatus;
}
private static string EvaluateCompoundConditions()
{
string branch;
#if (DEBUG_MODE && !VC_V10)
branch = "DEBUG_ONLY";
#elif (!DEBUG_MODE && VC_V10)
branch = "VC_V10_ONLY";
#elif (DEBUG_MODE && VC_V10)
branch = "DEBUG and VC_V10 are defined";
#else
branch = "NEITHER";
#endif
string opBranch;
#if ((DEBUG_MODE == true) && (TEMP_SYMBOL != true) && (PI || false))
opBranch = "OPS_TRUE";
#else
opBranch = "OPS_FALSE";
#endif
#if false
#error This error branch is excluded by #if false
#endif
return branch + "|" + opBranch;
}
private static string EvaluateLineDirective()
{
#line 100 "VirtualGenerated.cs"
string markedOutput = "This is line 100";
#line default
string restoredOutput = "Line numbering restored";
return string.Format("{0}|{1}", markedOutput, restoredOutput);
}
#endregion
private static string Esc(string s)
{
if (s == null) return "null";
StringBuilder sb = new StringBuilder("\"");
foreach (char c in s)
{
switch (c)
{
case '\\': sb.Append("\\\\"); break;
case '"': sb.Append("\\\""); break;
case '\n': sb.Append("\\n"); break;
case '\r': sb.Append("\\r"); break;
case '\t': sb.Append("\\t"); break;
default:
if (c < 32) sb.AppendFormat("\\u{0:x4}", (int)c);
else sb.Append(c);
break;
}
}
sb.Append("\"");
return sb.ToString();
}
private static string CaseJson(string name, string summary, string expected, string actual)
{
bool passed = string.Equals(expected, actual, StringComparison.Ordinal);
return string.Format(
"{{\"name\":{0},\"summary\":{1},\"expected\":{2},\"actual\":{3},\"passed\":{4}}}",
Esc(name), Esc(summary), Esc(expected), Esc(actual), passed ? "true" : "false"
);
}
private static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
List<string> cases = new List<string>();
// Case 1: #define and #undef
cases.Add(CaseJson(
"define_and_undef_symbols",
"验证文件顶部 #define 定义符号 PI 以及 #undef 取消 TEMP_SYMBOL 后的条件编译分支",
"PI is defined|TEMP_UNDEF",
EvaluateDefineAndUndef()
));
// Case 2: #if / #elif / #else / #endif with !, ==, !=, &&, || and parentheses
cases.Add(CaseJson(
"conditional_if_elif_else",
"验证 #if/#elif/#else/#endif 组合布尔表达式(!、==、!=、&&、|| 与括号分组)的编译期求值",
"DEBUG and VC_V10 are defined|OPS_TRUE",
EvaluateCompoundConditions()
));
// Case 3: #region/#endregion, #line/#line default, and #pragma warning disable/restore 414
Program p = new Program();
string lineInfo = EvaluateLineDirective();
string actual3 = string.Format("{0}|fieldOk={1}", lineInfo, p != null);
cases.Add(CaseJson(
"region_line_and_pragma",
"验证 #region/#endregion 代码折叠区域、#line/#line default 编译通过且正常执行,以及 #pragma warning disable/restore 414",
"This is line 100|Line numbering restored|fieldOk=True",
actual3
));
Console.WriteLine(string.Format(
"{{\"version\":{0},\"cases\":[{1}]}}",
Esc("CLR " + Environment.Version.ToString()),
string.Join(",", cases.ToArray())
));
}
}
}
本例需在 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。
入口文件:projects/csharp-preprocessor-directives-lab/ExplorePreprocessorDirectives.cs
本地运行步骤快速上手
- 点击上方“保存源码”保存为
ExplorePreprocessorDirectives.cs,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、.NET Framework 4.x Visual C# 编译器(csc.exe)本地离线环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联42章) · 含当前章节或实例 · mbskycom202612a1321.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1348.zip