欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中的两种debug方法介绍

程序员文章站 2023-12-11 20:43:34
第一种:需要把调试方法改成debug 代码用 #if debug 包裹 using system; using system.collections.gene...

第一种:需要把调试方法改成debug
代码用 #if debug 包裹

using system;
using system.collections.generic;
using system.text;
using system.io;

namespace splitpackage
{
  public static class envconfig
  {
    static envconfig()
    {
#if debug
      toolspath = @"d:\workspace\shopstyle\tool";
#else
      toolspath = environment.currentdirectory;
#endif
      int rootidx = toolspath.lastindexof(@"\");
      if (rootidx > 0)
      {
        rootpath = toolspath.substring(0, rootidx);
      }
    }
    public static string toolspath { get; private set; }
    public static string tmplatefile { get { return path.combine(toolspath, @"template\default.pm"); } }
    public static string rootpath { get; private set; }
    public static string modulepath { get { return path.combine(rootpath, "module"); } }
    public static string configpath { get { return path.combine(rootpath, "conf"); } }

  }
}

第二种:
利用宏定义

#define debug// c#的宏定义必须出现在所有代码之前。当前我们只让debug宏有效。
using system.diagnostics;  //必须包含这个包

#define debug

using system.diagnostics; 

namespace testconsole
{
  class toolkit
  {
    [conditionalattribute("li")]       // attribute名称的长记法
    [conditionalattribute("debug")]
    public static void method1() { console.writeline("created by li, buged.11"); }

    [conditionalattribute("li")]
    [conditionalattribute("nobug")]
    public static void method2() { console.writeline("created by li, nobug."); }

    [conditional("zhang")]          // attribute名称的短记法
    [conditional("debug")]
    public static void method3() { console.writeline("created by zhang, buged.11"); }

    [conditional("zhang")]
    [conditional("nobug")]
    public static void method4() { console.writeline("created by zhang, nobug."); }
  }
    static void main(string[] args)
    {
      toolkit.method1();
      toolkit.method2();
      toolkit.method3();
      toolkit.method4();
    }
  }
}

上一篇:

下一篇: