Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 13 章 诊断 诊断

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

当出现问题时,重要的是能获得有助于诊断问题的信息。集成开发环境 (IDE) 或调试器可以在很大程度上起到这种作用,但通常只能在开发过程中使用。应用程序发布后,应用程序本身必须收集和记录诊断信息。为了满足这一要求,.NET 提供了一系列工具来记录诊断信息、监控应用程序行为、检测运行时错误,并与可用的调试工具集成。

某些诊断工具和 API 是 Windows 专用的,因为它们依赖于 Windows 操作系统的功能。为了防止特定于平台的 API 给 .NET BCL 带来混乱,微软将它们放在单独的 NuGet包中,您可以选择引用这些包。有十几个 Windows 专用包,您可以使用Microsoft.Windows.Compatibility"master" 包一次性引用所有这些包。

本章中的类型主要在System.Diagnostics 命名空间中定义。

条件编译

您可以使用预处理器指令有条件地编译 C# 中的任何代码段。预处理器指令是给编译器的特殊指令,以# 符号开头(与其他 C# 结构不同,预处理器指令必须独立成行)。从逻辑上讲,它们在主编译之前执行(但实际上,编译器会在词法解析阶段处理它们)。用于条件编译的预处理器指令有#if,#else,#endif, 和#elif

#if 指令指示编译器忽略一段代码,除非已定义了指定的符号。您可以使用 指令在源代码中定义符号(在这种情况下,符号仅适用于该文件),也可以使用 元素在 #define <DefineConstants> .csproj文件中定义符号(在这种情况下,符号适用于整个程序集):

#define TESTMODE            // #define directives must be at top of file
                            // Symbol names are uppercase by convention.
using System;

class Program
{
  static void Main()
  {
#if TESTMODE
    Console.WriteLine ("in test mode!");     // OUTPUT: in test mode!
#endif
  }
}

如果我们删除了第一行,程序在编译时就会从可执行文件中完全删除Console.WriteLine 语句,就像删除了注释一样。

#else 语句类似于 C# 的 语句,而 相当于 之后的 。 、 和 操作符执行else #elif #else #if || && ! 操作

#if TESTMODE && !PLAYMODE      // if TESTMODE and not PLAYMODE
  ...

不过,请记住,您并不是在构建普通的 C# 表达式,您操作的符号与变量(静态变量或其他变量)完全无关。

您可以通过编辑.csproj文件(或在 Visual Studio 中进入 "项目属性 "窗口中的 "构建 "选项卡)来定义适用于程序集中每个文件的符号。下面定义了两个常量TESTMODEPLAYMODE

<PropertyGroup>
  <DefineConstants>TESTMODE;PLAYMODE</DefineConstants>
</PropertyGroup>

如果已在程序集级别定义了一个符号,但又想在特定文件中 "取消定义 "该符号,可以使用#undef 指令来实现。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038