MS的代码是这样的:
public static Form ActiveForm
{
get
{
IntSecurity.GetParent.Demand();
IntPtr ptr1 = UnsafeNativeMethods.GetForegroundWindow();
Control control1 = Control.FromHandleInternal(ptr1);
if ((control1 != null) && ((control1 as Form) != null))
{
return ((Form) control1);
}
return null;
}
}

为什么需要首先进行control1 != null 的判断呢?我怎么记得null as Form == null的,也就是说,难道不能够这么写吗?
public static Form ActiveForm
{
get
{
IntSecurity.GetParent.Demand();
IntPtr ptr1 = UnsafeNativeMethods.GetForegroundWindow();
Control control1 = Control.FromHandleInternal(ptr1);
if ((control1 as Form) != null)
{
return ((Form) control1);
}
return null;
}
}
还在好多地方看到一些问题,不一一列出来了。
public static Form ActiveForm
{
get
{
IntSecurity.GetParent.Demand();
IntPtr ptr1 = UnsafeNativeMethods.GetForegroundWindow();
Control control1 = Control.FromHandleInternal(ptr1);
if ((control1 != null) && ((control1 as Form) != null))
{
return ((Form) control1);
}
return null;
}
}

为什么需要首先进行control1 != null 的判断呢?我怎么记得null as Form == null的,也就是说,难道不能够这么写吗?
public static Form ActiveForm
{
get
{
IntSecurity.GetParent.Demand();
IntPtr ptr1 = UnsafeNativeMethods.GetForegroundWindow();
Control control1 = Control.FromHandleInternal(ptr1);
if ((control1 as Form) != null)
{
return ((Form) control1);
}
return null;
}
}还在好多地方看到一些问题,不一一列出来了。



浙公网安备 33010602011771号