nj.*_*nj. 20 c# compact-framework short-circuiting
在检查我的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();
}
Run Code Online (Sandbox Code Playgroud)
当我调用CameraPresent2()它时返回false(没有相机存在).但是,当我打电话给CameraPresent1()我收到一个MissingMethodException并注释"找不到方法:get_CameraEnabled Microsoft.WindowsMobile.Status.SystemState."
第二个术语的评估CameraPresent1是因为它们都是属性(在语言层面)吗?
还有什么能解释行为上的差异吗?
Ben*_*igt 31
第二个术语未经评估.
第一个术语未经评估.
该CameraPresent1()方法甚至没有开始执行.
当您CameraPresent1()第一次调用时,运行时必须将MSIL JIT编译为本机代码.这需要解析所有方法调用,即使是只能有条件地访问的方法调用.编译失败了MissingMethodException.
对于CameraPresent2()getter的调用CameraEnabled仅CameraE()在第一次调用时编译,这种情况从未发生过.
只是一个疯狂的猜测,但这可能是一个JIT编译问题?当调用CameraPresent1时,它是否尝试将调用Microsoft.WindowsMobile.Status.SystemState.CameraEnabled映射到底层设备?由于找不到方法get_CameraEnabled,整个函数失败并出现MissingMethodException.