C#中的#ifdef

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/975355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:39:29  来源:igfitidea点击:

#ifdef in C#

c#

提问by

I would like to do the below but in C# instead of C++

我想执行以下操作,但使用 C# 而不是 C++

#ifdef _DEBUG
bool bypassCheck=TRUE_OR_FALSE;//i will decide depending on what i am debugging
#else
bool bypassCheck = false; //NEVER bypass it
#endif

回答by heavyd

#if DEBUG
bool bypassCheck=TRUE_OR_FALSE;//i will decide depending on what i am debugging
#else
bool bypassCheck = false; //NEVER bypass it
#endif

Make sure you have the checkbox to define DEBUG checked in your build properties.

确保在构建属性中勾选了定义调试的复选框。

回答by Pavel Nikolov

I would recommend you using the Conditional Attribute!

我建议您使用条件属性

Update: 3.5 years later

更新:3.5 年后

You can use #iflike this (example copied from MSDN):

您可以#if像这样使用(从 MSDN 复制的示例):

// preprocessor_if.cs
#define DEBUG
#define VC_V7
using System;
public class MyClass 
{
    static void Main() 
    {
#if (DEBUG && !VC_V7)
        Console.WriteLine("DEBUG is defined");
#elif (!DEBUG && VC_V7)
        Console.WriteLine("VC_V7 is defined");
#elif (DEBUG && VC_V7)
        Console.WriteLine("DEBUG and VC_V7 are defined");
#else
        Console.WriteLine("DEBUG and VC_V7 are not defined");
#endif
    }
}

Only useful for excluding parts of methods.

仅用于排除部分方法。

If you use #ifto exclude some method from compilation then you will have to exclude from compilation all pieces of code which call that method as well (sometimes you may load some classes at runtime and you cannot find the caller with "Find all references"). Otherwise there will be errors.

如果您使用#if从编译中排除某些方法,那么您将不得不从编译中排除所有调用该方法的代码(有时您可能会在运行时加载某些类,并且您无法通过“查找所有引用”找到调用者)。否则会出现错误。

If you use conditional compilation on the other hand you can still leave all pieces of code that call the method. All parameters will still be validated by the compiler. The method just won't be called at runtime. I think that it is way better to hide the method just once and not have to remove all the code that calls it as well. You are not allowed to use the conditional attribute on methods which return value - only on void methods. But I don't think this is a big limitation because if you use #ifwith a method that returns a value you have to hide all pieces of code that call it too.

另一方面,如果您使用条件编译,您仍然可以保留调用该方法的所有代码段。所有参数仍将由编译器验证。该方法不会在运行时被调用。我认为最好只隐藏一次该方法,而不必删除所有调用它的代码。不允许在返回值的方法上使用条件属性 - 只能在 void 方法上使用。但我不认为这是一个很大的限制,因为如果您使用#if返回值的方法,您也必须隐藏调用它的所有代码段。

Here is an example:

下面是一个例子:

    // calling Class1.ConditionalMethod() will be ignored at runtime 
    // unless the DEBUG constant is defined


    using System.Diagnostics;
    class Class1 
    {
       [Conditional("DEBUG")]
       public static void ConditionalMethod() {
          Console.WriteLine("Executed Class1.ConditionalMethod");
       }
    }

Summary:

概括:

I would use #ifdefin C++ but with C#/VB I would use Conditional attribute. This way you hide the method definition without having to hide the pieces of code that call it. The calling code is still compiled and validated by the compiler, the method is not called at runtime though. You may want to use #ifto avoid dependencies because with Conditional attribute your code is still compiled.

我会#ifdef在 C++ 中使用,但在 C#/VB 中我会使用 Conditional 属性。这样您就可以隐藏方法定义,而不必隐藏调用它的代码段。调用代码仍然由编译器编译和验证,尽管在运行时不会调用该方法。您可能希望使用#if来避免依赖关系,因为使用 Conditional 属性,您的代码仍会被编译。

回答by Karl T.

C# does have a preprocessor. It works just slightly differently than that of C++ and C.

C# 确实有一个预处理器。它的工作原理与 C++ 和 C 略有不同。

Here is a MSDN links - the section on all preprocessor directives.

这是一个 MSDN 链接 - 关于所有预处理器指令的部分。