.Net 浏览器架构

.Net WebBrowser architecture

我正在使用 .Net webBrowser html 解析

在我的程序中,我有一个 MainForm(表单对象)和初始化 MainForm 的程序 class。

在表单中,我进行了 webBrowser 初始化,当我进入网站时,我需要处理不同的页面(从 it/download 文件获取数据等)

今天我把所有的案例都放在表格中,按区域分隔为函数:

public partial class MainForm : Form
{
   if(DoTypeA())
   {
      ...
   }
   ...
   #region TYPEA
   private bool DoTypeA()
   {
      HtmlDocument doc = webBrowser.Document ....
      ...
   }
   #endregion TYPEA
   ...
}

在类型 A 中,我重定向到页面并从中解析了大量数据

我可以在不同的 class 中分离该功能吗?使代码更清晰。

问题是 webBrowser 及其所有事件以 class.

的形式发生

对于这种情况,最好的设计模式/架构模式是什么。

我尝试了什么:

我看到的唯一解决方案是将 webBrowser 作为参数传递给 DoTypeA(webBrowser); 但我可以不传递参数吗?

谢谢。

我想到的解决方案之一,就是DI模式

class Types
{
   IWebDriver webBrowser = null;

   public Types(IWebDriver wb)
   {
      if(wb == null)
         throw new NullReferenceException();

      this.webBrowser = wb;
   }

   public bool DoTypeA()
   {
       HtmlDocument doc = webBrowser.Document //working fine
   }
}

来自 MainForm 的调用

Types dt = new Types(webBrowser);
if(dt.GetTypeA())
{
   //done.
}