在编写代码时
var e = element.NextSibling as HtmlElement;
我遇到了一次无效的强制类型转换异常。我以为使用“as”关键字可以防止强制类型转换异常并返回null?因为这正是我期望的。请告诉我,如何使e为null而不是抛出异常?或者当使用“as”关键字时为什么会出现此异常?
-编辑- 更正一下,抛出异常的是element.NextSibling。很奇怪的是,在我还没有尝试进行强制类型转换时,它就抛出了一个强制类型转换异常。我不确定如何检查是否存在下一个兄弟节点。NextSibling中的错误如下:
Message "Unable to cast COM object of type 'System.__ComObject' to interface type 'IHTMLElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F1FF-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))." string