116 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
Run Code Online (Sandbox Code Playgroud)
hea*_*vyd 161
#if DEBUG
bool bypassCheck=TRUE_OR_FALSE;//i will decide depending on what i am debugging
#else
bool bypassCheck = false; //NEVER bypass it
#endif
Run Code Online (Sandbox Code Playgroud)
确保您具有用于在构建属性中选中DEBUG的复选框.
Pav*_*lov 50
我建议你使用条件属性!
更新:3.5年后
您可以#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
}
}
Run Code Online (Sandbox Code Playgroud)
仅对排除部分方法有用.
如果您使用#if从编译中排除某些方法,那么您将不得不从编译中排除调用该方法的所有代码片段(有时您可能在运行时加载某些类,并且您无法通过"查找所有引用"找到调用者).否则会有错误.
另一方面,如果使用条件编译,您仍然可以保留调用该方法的所有代码片段.所有参数仍将由编译器验证.该方法不会在运行时调用.我认为隐藏方法一次更好,而不必删除所有调用它的代码.不允许在返回值的方法上使用条件属性 - 仅限于void方法.但我不认为这是一个很大的限制,因为如果你使用#if一个返回值的方法,你必须隐藏所有调用它的代码片段.
这是一个例子:
// 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");
}
}
摘要:
我会#ifdef在C++中使用但是使用C#/ VB我会使用Conditional属性.这样,您可以隐藏方法定义,而无需隐藏调用它的代码片段.调用代码仍由编译器编译和验证,但该方法在运行时不会被调用.您可能希望使用#if以避免依赖性,因为使用Conditional属性仍然会编译代码.
| 归档时间: |
|
| 查看次数: |
128850 次 |
| 最近记录: |