#if DEBUG vs. Conditional(“DEBUG") [英] #if DEBUG vs. Conditional("DEBUG")

查看:22
本文介绍了#if DEBUG vs. Conditional(“DEBUG")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大型项目中哪个更好用,为什么:

Which is better to use, and why, on a large project:

#if DEBUG
    public void SetPrivateValue(int value)
    { ... }
#endif

[System.Diagnostics.Conditional("DEBUG")]
public void SetPrivateValue(int value)
{ ... }

推荐答案

这真的取决于你的目的:

It really depends on what you're going for:

  • #if DEBUG:这里的代码在发布时甚至不会到达 IL.
  • [Conditional("DEBUG")]:此代码将到达IL,但是除非在编译调用者时设置了DEBUG,否则将省略对该方法的调用.
  • #if DEBUG: The code in here won't even reach the IL on release.
  • [Conditional("DEBUG")]: This code will reach the IL, however calls to the method will be omitted unless DEBUG is set when the caller is compiled.

我个人根据情况使用两者:

Personally I use both depending on the situation:

Conditional("DEBUG") 示例:我使用它是为了以后在发布期间我不必返回并编辑我的代码,但在调试期间我想确定我没有'不要打错字.当我尝试在 INotifyPropertyChanged 内容中使用它时,此函数会检查我是否正确键入了属性名称.

Conditional("DEBUG") Example: I use this so that I don't have to go back and edit my code later during release, but during debugging I want to be sure I didn't make any typos. This function checks that I type a property name correctly when trying to use it in my INotifyPropertyChanged stuff.

[Conditional("DEBUG")]
[DebuggerStepThrough]
protected void VerifyPropertyName(String propertyName)
{
    if (TypeDescriptor.GetProperties(this)[propertyName] == null)
        Debug.Fail(String.Format("Invalid property name. Type: {0}, Name: {1}",
            GetType(), propertyName));
}

您真的不想使用 #if DEBUG 创建函数,除非您愿意使用相同的 #if DEBUG 包装对该函数的每个调用:

You really don't want to create a function using #if DEBUG unless you are willing to wrap every call to that function with the same #if DEBUG:

#if DEBUG
    public void DoSomething() { }
#endif

    public void Foo()
    {
#if DEBUG
        DoSomething(); //This works, but looks FUGLY
#endif
    }

对比:

[Conditional("DEBUG")]
public void DoSomething() { }

public void Foo()
{
    DoSomething(); //Code compiles and is cleaner, DoSomething always
                   //exists, however this is only called during DEBUG.
}

<小时>

#if DEBUG 示例: 我在尝试为 WCF 通信设置不同绑定时使用它.


#if DEBUG example: I use this when trying to setup different bindings for WCF communication.

#if DEBUG
        public const String ENDPOINT = "Localhost";
#else
        public const String ENDPOINT = "BasicHttpBinding";
#endif

在第一个示例中,代码全部存在,但除非打开 DEBUG,否则将被忽略.在第二个示例中,const ENDPOINT 设置为Localhost"或BasicHttpBinding",具体取决于是否设置了 DEBUG.

In the first example, the code all exists, but is just ignored unless DEBUG is on. In the second example, the const ENDPOINT is set to "Localhost" or "BasicHttpBinding" depending on if DEBUG is set or not.

更新:我正在更新此答案以澄清一个重要且棘手的问题.如果您选择使用 ConditionalAttribute,请记住,在编译期间会省略调用,而运行时会忽略.即:

Update: I am updating this answer to clarify an important and tricky point. If you choose to use the ConditionalAttribute, keep in mind that calls are omitted during compilation, and not runtime. That is:

MyLibrary.dll

MyLibrary.dll

[Conditional("DEBUG")]
public void A()
{
    Console.WriteLine("A");
    B();
}

[Conditional("DEBUG")]
public void B()
{
    Console.WriteLine("B");
}

当库针对发布模式(即没有调试符号)编译时,它将永远忽略从 A() 内部调用 B(),即使如果包含对 A() 的调用,因为在调用程序集中定义了 DEBUG.

When the library is compiled against release mode (i.e. no DEBUG symbol), it will forever have the call to B() from within A() omitted, even if a call to A() is included because DEBUG is defined in the calling assembly.

这篇关于#if DEBUG vs. Conditional(“DEBUG")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆