(原創) 使用preprocessor directive留住debug code (.NET) (C#)

Abstract
很多人以為debug mode和release mode的差異只是debug mode可以設Breakpoint而已,事實上,搭配preprocessor directive,debug mode另有妙用。

Introduction
preprocessor directive並不是什麼新東西,這在C語言就有的,如有些API在Windows 98和Windows XP不一樣,就得用preprocessor directive,讓不同的平台用不同的API。C#也可使用preprocessor directive,尤其用在debug時,非常方便。

我們常會有debug code,如try catch時,若有exception要顯示錯誤訊息,但真正發布產品時,則不希望顯示錯誤訊息,所以希望能留住debug code,以便日後debug,若用//或/* */的方式將debug code暫時當註解,常常遇到產品真正發布時,忘了將debug code拿掉的窘境,事實上,當使用debug mode時,C#自動定義了

None.gif #define  DEBUG


所以我們可以用#if (DEBUG)來留住debug code。

Example

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /* 
 2InBlock.gif(C) OOMusou 2007 http://oomusou.cnblogs.com
 3InBlock.gif
 4InBlock.gifFilename    : Debug.cs
 5InBlock.gifCompiler    : Visual Studio 2005 / C# 2.0
 6InBlock.gifDescription : Demo how to use preprocessor to debug
 7InBlock.gifRelease     : 07/10/2007 1.0
 8ExpandedBlockEnd.gif*/

 9 None.gif using  System;
10 None.gif using  System.IO;
11 None.gif using  System.Collections.Generic;
12 None.gif
13 ExpandedBlockStart.gifContractedBlock.gif class  Client  dot.gif {
14ExpandedSubBlockStart.gifContractedSubBlock.gif  static bool fileToList(string fileName, List<string> list) dot.gif{
15InBlock.gif    StreamReader reader = null;
16InBlock.gif
17ExpandedSubBlockStart.gifContractedSubBlock.gif    try dot.gif{
18InBlock.gif      reader = new StreamReader(fileName);
19InBlock.gif      string line = reader.ReadLine();
20InBlock.gif
21ExpandedSubBlockStart.gifContractedSubBlock.gif      while (line != nulldot.gif{
22InBlock.gif        list.Add(line);
23InBlock.gif        line = reader.ReadLine();
24ExpandedSubBlockEnd.gif      }

25ExpandedSubBlockEnd.gif    }

26InBlock.gif    #if (DEBUG)
27ExpandedSubBlockStart.gifContractedSubBlock.gif    catch(Exception e) dot.gif{
28InBlock.gif      Console.WriteLine(e.Message);
29InBlock.gif    #else
30ExpandedSubBlockStart.gifContractedSubBlock.gif    catch dot.gif{
31InBlock.gif    #endif
32InBlock.gif      return false;
33ExpandedSubBlockEnd.gif    }

34ExpandedSubBlockStart.gifContractedSubBlock.gif    finally dot.gif{
35InBlock.gif      if (reader != null)
36InBlock.gif        reader.Close();
37ExpandedSubBlockEnd.gif    }

38InBlock.gif
39InBlock.gif    return true;
40ExpandedSubBlockEnd.gif  }

41InBlock.gif
42ExpandedSubBlockStart.gifContractedSubBlock.gif  static int Main(string[] args) dot.gif{
43InBlock.gif    string fileName = "ReadMe.txt";
44InBlock.gif    List<string> list = new List<string>();
45InBlock.gif
46InBlock.gif    if (!fileToList(fileName, list))
47InBlock.gif      return 1;
48InBlock.gif
49InBlock.gif    foreach (string line in list)
50InBlock.gif      Console.WriteLine(line);
51InBlock.gif
52InBlock.gif    return 0;
53ExpandedSubBlockEnd.gif  }

54ExpandedSubBlockEnd.gif}


執行結果(Debug Mode)

None.gif Could not find file 'D:\__Clare\CSharp\CSharpLab\bin\Debug\ReadMe.txt'.
None.gif請按任意鍵繼續 . . .


執行結果(Release Mode)

None.gif 請按任意鍵繼續 . . .


26到33行

None.gif #if  (DEBUG)
ExpandedBlockStart.gifContractedBlock.gif  
catch (Exception e)  dot.gif {
InBlock.gif  Console.WriteLine(e.Message);
InBlock.gif
#else
ExpandedSubBlockStart.gifContractedSubBlock.gif  
catch dot.gif{
InBlock.gif
#endif
InBlock.gif  
return false;
ExpandedSubBlockEnd.gif}


我們希望在debug mode時,能顯示exception message,但release mode時則不顯示,若用#if (DEBUG)來寫,再也不用擔心debug code忘記拿掉的問題,只要切換debug mode和release mode,就可輕鬆顯示debug code,而且Visual Studio 2005也會在切換debug和release時,動態改變code的顏色,讓你立刻知道哪些code會執行到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值