当前位置: 移动技术网 > IT编程>开发语言>c# > C#中的两种debug方法介绍

C#中的两种debug方法介绍

2019年07月18日  | 移动技术网IT编程  | 我要评论

第一种:需要把调试方法改成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();
    }
  }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网