gpt4 book ai didi

c# - 为什么短路不能阻止与逻辑 AND (&&) 的不可到达分支相关的 MissingMethodException?

转载 作者:IT王子 更新时间:2023-10-29 04:37:06 25 4
gpt4 key购买 nike

在检查我的 Windows 移动设备上是否存在和启用摄像头时,我遇到了一些我不明白的事情。

代码如下所示:

    public static bool CameraP(){

return Microsoft.WindowsMobile.Status.SystemState.CameraPresent;
}

public static bool CameraE()
{
return Microsoft.WindowsMobile.Status.SystemState.CameraEnabled;
}

public static bool CameraPresent1()
{
return Microsoft.WindowsMobile.Status.SystemState.CameraPresent
&& Microsoft.WindowsMobile.Status.SystemState.CameraEnabled;
}

public static bool CameraPresent2()
{
return CameraP() && CameraE();
}

当我调用 CameraPresent2() 时,它返回 false(没有摄像头)。但是当我调用 CameraPresent1() 时,我收到 MissingMethodException 并附有注释“找不到方法:get_CameraEnabled Microsoft.WindowsMo​​bile.Status.SystemState。”

CameraPresent1 中评估第二项是否只是因为它们都是属性(在语言级别)?

还有什么可以解释行为上的差异吗?

最佳答案

不计算第二项。

不评估第一项。

CameraPresent1() 方法甚至没有开始执行。

当您第一次调用 CameraPresent1() 时,运行时必须将 MSIL JIT 编译为 native 代码。这需要解决所有方法调用,甚至是可能仅有条件地达到的方法调用。编译失败并出现 MissingMethodException

使用CameraPresent2(),调用CameraEnabled的getter只在第一次调用CameraE()时编译,这永远不会发生。

关于c# - 为什么短路不能阻止与逻辑 AND (&&) 的不可到达分支相关的 MissingMethodException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5233155/

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