IAM到无法转换类型的“HtmlAgilityPack.HtmlDocument”对象为类型“mshtml.IHTMLDocument2”无法投射型“HtmlAgilityPack.HtmlDocument”的目的为类型“mshtml.IHTMLDocument2”
HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
IAM到无法转换类型的“HtmlAgilityPack.HtmlDocument”对象为类型“mshtml.IHTMLDocument2”无法投射型“HtmlAgilityPack.HtmlDocument”的目的为类型“mshtml.IHTMLDocument2”
HTMLDocument doc = new HTMLDocument();
IHTMLDocument2 doc2 = (IHTMLDocument2)doc;
它是因为HTMLDocument
未执行IHTMLDocument2
,因此没有已知路径来执行强制转换。
实施没有必要 – chandu
在这种情况下,这是因为没有明确的运算符定义执行转换。如果从object1投射到object2,那么必须在继承,实现或操作符之类的类型之间存在某种关系,以说明转换应如何工作。 –
为了铸铅字A
键入B
,下列之一应该是真实的
A
应该从B
类被继承,或者如果它是一个接口,它应该实现类型B
。在这种情况下,隐式转换将起作用B
应该从类型A
继承,并且您正在转换的变量应该包含B
类型的实例。,那么你会得到一个运行时异常。对于类型HtmlAgilityPack.HtmlDocument
和mshtml.IHTMLDocument2
,两个条件都不满足。
UPDATE:好像你有一个错字 - 而不是声明doc
变量HTMLDocument
你已经宣布它为HtmlDocument
。 C#是一个区分大小写的语言。
看起来像错误很自我描述。你可以不投A型B型,就像你不能用这种方式,我们可以投的system.windowsforms.document 到html的IHTMLDocument2投布尔最新 –
的IHTMLDocument2 DOC =(的IHTMLDocument2)webBrowserDomDocument.Document.DomDocument; 它是可能的,但我是投在HTML AgilityP ack也 – chandu