为什么短路不能防止与逻辑 AND (&&) 的不可到达分支相关的 MissingMethodException?
在检查我的 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.WindowsMobile.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不评估第二项。
第一项不进行评估。
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 theMissingMethodException
.With
CameraPresent2()
, the call to the getter ofCameraEnabled
is only compiled whenCameraE()
is called for the first time, which never happens.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 ifCameraP()
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 operationx & y
, except thaty
is evaluated only ifx
is notfalse
.The operation
x || y
corresponds to the operationx | y
, except thaty
is evaluated only ifx
is nottrue
.That is to say that the C# spec guarantees that
CameraE()
will be called if and only ifCameraP()
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?
只是一个大胆的猜测,但这有可能是 JIT 编译问题吗?调用 CameraPresent1 时,它是否尝试将调用 Microsoft.WindowsMobile.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.
从报道的问题来看,似乎没有任何意义。两个版本应该是相同的。不过,我想知道这里的问题是否是相机 API 在某个时刻使用
dynamic
,并且它正在尝试寻找true()
/false()
/&
运算符。这可能会说服它切换到 bool 逻辑: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 atrue()
/false()
/&
operator. This might convince it to switch tobool
logic: