iam to Unable to cast object of type 'HtmlAgilityPack.HtmlDocument' to type 'mshtml.IHTMLDocument2'
HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
iam to Unable to cast object of type 'HtmlAgilityPack.HtmlDocument' to type 'mshtml.IHTMLDocument2'
HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
In order to cast type A
to type B
, one of following should be true
A
should be inherited from type B
, or it should implement type B
if it's an interface. Implicit conversion will work in this caseB
should be inherited from type A
and variable which you are casting should hold an instance of type B
.If none is true, then you'll get a runtime exception. For types HtmlAgilityPack.HtmlDocument
and mshtml.IHTMLDocument2
both conditions are not satisfied.
UPDATE: Seems like you have a typo - instead of declaring doc
variable as HTMLDocument
you have declared it as HtmlDocument
. C# is a case-sensitive language.
Further reading: Casting and Type Conversions (C# Programming Guide)
It is because HTMLDocument
does not implement IHTMLDocument2
so there's no known path to perform the cast.