false当它不是 a 时得到WebControl,true否则:
bool isWebControl = validateControl is WebControl;
null当它不是 a 时得到WebControl,WebControl否则得到:
WebControl webControl = validateControl as WebControl;
可以(validateControl as WebControl)为空吗?
是的,每次使用时as,理论上结果可能为空。代码分析工具看不到您刚刚检查它是否不为空,并且仍会假设下一次使用的as可能为空。所以你应该把它放在一个变量中并使用它来代替:
WebControl webControl = validateControl as WebControl;
if (webControl != null)
{
// Here 'webControl' is surely _not_ null.
webControl.CssClass = Page.IsValid ? "stack" : "overflow";
}
可以(validateControl as WebControl).CssClass为空吗?
您从中获得的值CssClass可能为空。但是既然CssClass是一个属性,那么只要是一个属性,这个属性就会一直validateControl存在WebControl。