C#赢得';无法识别getAttribute

本文关键字:识别 getAttribute 赢得 | 更新日期: 2023-09-27 18:30:14

我将VB.Net应用程序转移到C#,并在构建它时显示此错误-

"mshtml.IHMLImgElement"不包含的定义"getattribute"且没有接受找不到类型为"mshtml.IHMLImgElement"的第一个参数(分别为缺少using指令或程序集引用?)

那么我该如何替换这个getattribute调用呢?

这是代码

private void captcha()
{
   mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2) WebBrowser1.Document.DomDocument;
   mshtml.IHTMLControlRange imgrange = (mshtml.IHTMLControlRange) (((mshtml.HTMLBody) doc.body).createControlRange());
   foreach (mshtml.IHTMLImgElement img in doc.images)
   {
      if (img.getattribute("src").ToString().Contains("urltoimg"))
      {
         imgrange.add((mshtml.IHTMLControlElement) img);
         imgrange.execCommand("copy", false, null);
         PictureBox1.Image = (System.Drawing.Image) (Clipboard.GetDataObject().GetData(DataFormats.Bitmap));
         break;
      }
   }
}

C#赢得';无法识别getAttribute

尝试"GetAttribute"。VB不区分大小写,所以"getattribute"在VB中可以工作,但C#(以及几乎所有其他语言)是区分大小写的。

您需要将mshtml.IHTMLImgElement强制转换为mshtml.IHTMLElement,这将为您提供getAttribute()方法

您可以将此行替换为:

if (((mshtml.IHTMLElement)img.getAttribute("src")).ToString().Contains("urltoimg"))

注意大小写为"getAttribute",而不是"getAttribute"。