为什么短路不能阻止MissingMethodException与逻辑AND(&&)的不可达分支相关? [英] Why does short-circuiting not prevent MissingMethodException related to unreachable branch of logical AND (&&)?

查看:135
本文介绍了为什么短路不能阻止MissingMethodException与逻辑AND(&&)的不可达分支相关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在检查是否有摄像头并在Windows移动设备上启用了相机时,遇到了我不明白的事情。

While performing a check if there's a camera present and enabled on my windows mobile unit I encountered something I don't understand.

代码如下:

    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。

When I call CameraPresent2() it return false (there is no camera present). But when I call CameraPresent1() i recieve a MissingMethodException with comment "Could not find method: get_CameraEnabled Microsoft.WindowsMobile.Status.SystemState."

CameraPresent1 中是否仅因为它们都是财产(在语言级别)而评估了第二项?

Is the second term evaluated in CameraPresent1 just because they both are property (at language level)?

还有其他解释行为差异的东西吗?

Is there anything else that explains the difference in behaviour?

推荐答案

第二项未评估。

第一个条件未评估。

CameraPresent1()方法甚至不会开始执行。

The CameraPresent1() method does not even start to execute.

首次调用 CameraPresent1()时,运行时必须将MSIL JIT编译为本地代码。这要求解决所有方法调用,甚至可能只有有条件才能到达的方法调用。使用 MissingMethodException 编译失败。

When you call CameraPresent1() for the first time, the runtime must JIT-compile the MSIL into native code. This requires resolving all method calls, even ones that might be reached only conditionally. Compilation fails with the MissingMethodException.

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

With CameraPresent2(), the call to the getter of CameraEnabled is only compiled when CameraE() is called for the first time, which never happens.

这篇关于为什么短路不能阻止MissingMethodException与逻辑AND(&&)的不可达分支相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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