在解决方案属性中,我已经将我唯一的项目的配置设置为“发布”。

在主例程的开头,我有这样的代码,它显示“Mode=Debug”。 我在最上面还有这两行:

#define DEBUG 
#define RELEASE

我测试的变量对吗?

#if (DEBUG)
            Console.WriteLine("Mode=Debug"); 
#elif (RELEASE)
            Console.WriteLine("Mode=Release"); 
#endif

我的目标是根据调试和发布模式为变量设置不同的默认值。


如果您试图使用为构建类型定义的变量,您应该删除这两行…

#define DEBUG  
#define RELEASE 

... 这将导致#if (DEBUG)始终为真。

RELEASE也没有默认的条件编译符号。如果您想定义一个转到项目属性,单击Build选项卡,然后将RELEASE添加到General标题下的Conditional编译符号文本框中。

另一个选择是这样做……

#if DEBUG
    Console.WriteLine("Debug");
#else
    Console.WriteLine("Release");
#endif

默认情况下,如果项目在调试模式下编译,Visual Studio定义DEBUG,如果项目在发布模式下编译,则不定义DEBUG。默认情况下,RELEASE模式中没有定义RELEASE。可以这样说:

#if DEBUG
  // debug stuff goes here
#else
  // release stuff goes here
#endif

如果你只想在发布模式下做某事:

#if !DEBUG
  // release...
#endif

此外,值得指出的是,您可以在返回void的方法上使用[Conditional("DEBUG")]属性,以便仅在定义了某个符号时执行它们。如果符号没有定义,编译器将删除对这些方法的所有调用:

[Conditional("DEBUG")]
void PrintLog() {
    Console.WriteLine("Debug info");
}

void Test() {
    PrintLog();
}

DEBUG/_DEBUG应该已经在VS中定义了。

在代码中删除#define DEBUG。在构建配置中为特定的构建设置预处理器。

它打印“Mode=Debug”的原因是因为你的#define,然后跳过elif。

正确的检查方法是:

#if DEBUG
    Console.WriteLine("Mode=Debug"); 
#else
    Console.WriteLine("Mode=Release"); 
#endif

不要检查RELEASE。


删除顶部的定义

#if DEBUG
        Console.WriteLine("Mode=Debug"); 
#else
        Console.WriteLine("Mode=Release"); 
#endif

我更喜欢这样检查它,而不是寻找#define指令:

if (System.Diagnostics.Debugger.IsAttached)
{
   //...
}
else
{
   //...
}

需要注意的是,您当然可以在调试模式下编译和部署一些东西,但仍然没有附加调试器。


名称空间

using System.Resources;
using System.Diagnostics;

方法

   private static bool IsDebug()
    {
        object[] customAttributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(DebuggableAttribute), false);
        if ((customAttributes != null) && (customAttributes.Length == 1))
        {
            DebuggableAttribute attribute = customAttributes[0] as DebuggableAttribute;
            return (attribute.IsJITOptimizerDisabled && attribute.IsJITTrackingEnabled);
        }
        return false;
    }

我不是#if之类的东西的超级粉丝,特别是如果你把它散布在你的代码库中,因为如果你不小心,它会给你带来调试构建通过但发布构建失败的问题。

下面是我想出的(灵感来自c#中的#ifdef):

public interface IDebuggingService
{
    bool RunningInDebugMode();
}

public class DebuggingService : IDebuggingService
{
    private bool debugging;

    public bool RunningInDebugMode()
    {
        //#if DEBUG
        //return true;
        //#else
        //return false;
        //#endif
        WellAreWe();
        return debugging;
    }

    [Conditional("DEBUG")]
    private void WellAreWe()
    {
        debugging = true;
    }
}

Since the purpose of these COMPILER directives are to tell the compiler NOT to include code, debug code,beta code, or perhaps code that is needed by all of your end users, except say those the advertising department, i.e. #Define AdDept you want to be able include or remove them based on your needs. Without having to change your source code if for example a non AdDept merges into the AdDept. Then all that needs to be done is to include the #AdDept directive in the compiler options properties page of an existing version of the program and do a compile and wa la! the merged program's code springs alive!.

对于还没有准备好进入黄金时段的新流程,或者在发布之前不能在代码中活动的新流程,您可能还想使用声明式。

总之,我就是这么做的。


bool isDebug = false;
Debug.Assert(isDebug = true); // '=', not '=='

方法Debug。Assert具有条件属性DEBUG。如果没有定义,则取消调用和isDebug = true赋值:

如果定义了符号,则包含调用;否则,调用(包括调用参数的计算)将被省略。

如果定义了DEBUG,则isDebug将被设置为true(并传递给DEBUG)。Assert,在这种情况下它什么也不做)。


对Tod Thomson的答案稍加修改(私生子化?),将其作为一个静态函数而不是一个单独的类(我希望能够从我已经包含的viewutils类中调用它)。

public static bool isDebugging() {
    bool debugging = false;

    WellAreWe(ref debugging);

    return debugging;
}

[Conditional("DEBUG")]
private static void WellAreWe(ref bool debugging)
{
    debugging = true;
}

一个可以为您节省大量时间的技巧-不要忘记,即使您在构建配置下选择了调试(在vs2012/13菜单中,它在build => configuration MANAGER下)-这是不够的。

你需要注意PUBLISH配置,比如:


确保在“项目生成属性”中定义了DEBUG常量。这将启用#if DEBUG。我没有看到预定义的RELEASE常量,所以这可能意味着任何不在DEBUG块中的东西都是RELEASE模式。


我得想个更好的办法。我明白了,#if块是其他配置中的有效注释(假设DEBUG或RELEASE;但任何符号都适用)

public class Mytest
    {
        public DateTime DateAndTimeOfTransaction;
    }

    public void ProcessCommand(Mytest Command)
        {
            CheckMyCommandPreconditions(Command);
            // do more stuff with Command...
        }

        [Conditional("DEBUG")]
        private static void CheckMyCommandPreconditions(Mytest Command)
        {
            if (Command.DateAndTimeOfTransaction > DateTime.Now)
                throw new InvalidOperationException("DateTime expected to be in the past");
        }

删除定义并检查条件是否处于调试模式。你不需要检查指令是否处于释放模式。

就像这样:

#if DEBUG
     Console.WriteLine("Mode=Debug"); 
#else
    Console.WriteLine("Mode=Release"); 
#endif

It is worth noting here that one of the most significant differences between conditionally executing code based on #if DEBUG versus if(System.Diagnostics.Debugger.IsAttached) is that the compiler directive changes the code that is compiled. That is, if you have two different statements in an #if DEBUG/#else/#endif conditional block, only one of them will appear in the compiled code. This is an important distinction because it allows you do do things such as conditionally compile method definitions to be public void mymethod() versus internal void mymethod() depending on build type so that you can, for example, run unit tests on debug builds that will not break access control on production builds, or conditionally compile helper functions in debug builds that will not appear in the final code if they would violate security in some way should they escape into the wild. The IsAttached property, on the other hand, does not affect the compiled code. Both sets of code are in all of the builds - the IsAttached condition will only affect what is executed. This by itself can present a security issue.