gpt4 book ai didi

c# - 使用 ConditionalAttribute 是否也会删除参数计算?

转载 作者:可可西里 更新时间:2023-11-01 08:34:39 24 4
gpt4 key购买 nike

我尝试了以下代码:

class Magic {
[Conditional("DEBUG")]
public static void DoMagic( int stuff )
{
}
public static int ComputeMagic()
{
throw new InvalidOperationException();
}
}
class Program
{
static void Main(string[] args)
{
Magic.DoMagic(Magic.ComputeMagic());
}
}

看起来在 Release 构建中没有抛出异常,因此不仅删除了对标记有 ConditionalAttribute 的方法的调用,而且还消除了参数计算。

这样的行为有保障吗?

最佳答案

是的,当 DEBUG 未定义时(这在 Release 构建中很典型),参数评估被移除。

来自 https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#reserved-attributes :

If the symbol is defined, the call is included; otherwise, the call(including evaluation of the parameters of the call) is omitted.

关于c# - 使用 ConditionalAttribute 是否也会删除参数计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21935452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com