简体   繁体   中英

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'

I am getting the exception below when casting an object as IEnumerable.

This is part of my automation project which was built using VS2008 and was meant to run on IE8, now I am trying to use VS2010 and run on IE9.

System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{496B0ABE-CDEE-11D3-88E8-00902754C43A}' failed due to the following error: 'No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))' and the COM component does not support IDispatch::Invoke calls for DISPID_NEWENUM.

 HTMLDocument doc = some htmldocument
 IHTMLElement ele = doc.getElementById("some property");

 int iTab = (int)ele.getAttribute("someproperty", 0);
 object oTab = ele.getAttribute("property", 1);

 IEnumerable xyz = (IEnumerable)oTab;
 System.Collections.IEnumerator index = xyz.GetEnumerator();

It's pretty much as the error message says. The object in oTab does not implement IEnumerable . Whatever reasoning led you to assume that it did, is wrong.

Start by inspecting the value of oTab .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM