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

发布于 2024-10-21 00:11:37 字数 938 浏览 6 评论 0原文

在检查我的 Windows Mobile 设备上是否存在并启用相机时,我遇到了一些我不明白的事情。

代码如下所示:

    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 中评估第二项是否只是因为它们都是属性(在语言级别)?

还有其他什么可以解释行为差异吗?

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

The code looks like this:

    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();
    }

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."

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇俏 2024-10-28 00:11:37

不评估第二项。

第一项不进行评估。

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

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

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

The second term is not evaluated.

The first term is not evaluated.

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

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.

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

dawn曙光 2024-10-28 00:11:37

C# 规范第 7.12 节

& 和 < code>|| 运算符称为条件逻辑运算符。它们也称为“短路”逻辑运算符。

&&|| 运算符是 & 的条件版本。和 |运算符:

  • 运算x && y 对应于操作x & y,但仅当 x 不为 false 时才计算 y

  • 操作x || y 对应于操作x | y,但仅当 x 不为 true 时才计算 y


That is to say that the C# spec guarantees that CameraE() will be called if and only if CameraP() is true.

这可能是积极的编译器优化的问题,因此实际的程序似乎违反了语言规范......


Edit:

是否可以设置断点并显示反汇编窗口,以查看生成的确切代码?

C# Specification section 7.12

The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.

The && and || operators are conditional versions of the & and | operators:

  • The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is not false.

  • The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is not true.


That is to say that the C# spec guarantees that CameraE() will be called if and only if CameraP() is true.

This may be an issue with aggressive compiler optimizations, and therefore the actual program seems to violate the language spec...


Edit:

Is it possible to put up a breakpoint and show the disassembly window, to see the exact code generated?

心碎无痕… 2024-10-28 00:11:37

只是一个大胆的猜测,但这有可能是 JIT 编译问题吗?调用 CameraPresent1 时,它是否尝试将调用 Microsoft.WindowsMo​​bile.Status.SystemState.CameraEnabled 映射到底层设备?由于找不到方法 get_CameraEnabled,整个函数失败并出现 MissingMethodException。

Just a wild guess, but is it possible that this is a JIT compilation issue? When CameraPresent1 is called, is it attempting to map the call Microsoft.WindowsMobile.Status.SystemState.CameraEnabled to the underlying device? Since it cannot find the method get_CameraEnabled, the entire function fails with a MissingMethodException.

飞烟轻若梦 2024-10-28 00:11:37

从报道的问题来看,似乎没有任何意义。两个版本应该是相同的。不过,我想知道这里的问题是否是相机 API 在某个时刻使用 dynamic,并且它正在尝试寻找 true()/false()/& 运算符。这可能会说服它切换到 bool 逻辑:

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

Looking at the problem as reported, it seems to make no sense. The two versions should be identical. I wonder, though, if the problem here is that the camera API is using dynamic at some point, and it is trying to look for a true()/false()/& operator. This might convince it to switch to bool logic:

public static bool CameraPresent1()
{
    return ((bool)Microsoft.WindowsMobile.Status.SystemState.CameraPresent)
          && ((bool)Microsoft.WindowsMobile.Status.SystemState.CameraEnabled);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文