- 使用時機:.Net Framework內定的Section不符合使用時
1、新增一個Departement區段,代表應用程式的使用部門
2、使用類別物件(新的Class),以便於後續開發者方便存取這個設定檔的內容
Public Class Department
Public Name As String
Public Manager As String
End Class
3、建立一個實作IConfigurationSectionHandler介面的類別(新的Class),用來實作IConfigurationSectionHandler介面。使用Create方法,以取得組態區段中的attribute、element。
Imports System.Configuration
Public Class DepartmentHandler
Implements IConfigurationSectionHandler
Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
Dim dept As New Department
dept.Name = section.SelectSingleNode("Name").InnerText
dept.Manager = section.SelectSingleNode("Manager").InnerText
Return dept
End Function
End Class
4、必須在專案中的app.Config組態檔的區段中宣告Department型別
"Mod08_Section.DepartmentHandler,Mod08_Section"
專案名稱.處理Department類別名稱,專案名稱
5、取出自訂Section區段,使用Confurationmanager類別的GetSection方法取的Department區段再轉型為Department類別,即可使用存取
Imports System.Configuration
Dim dep As Department = CType(ConfigurationManager.GetSection("Department"), Department)
TextBox1.Text = dep.Name
TextBox2.Text = dep.Manager
Public Name As String
Public Manager As String
End Class
3、建立一個實作IConfigurationSectionHandler介面的類別(新的Class),用來實作IConfigurationSectionHandler介面。使用Create方法,以取得組態區段中的attribute、element。
- 第一個參數為父項物件,型別為Object
- 第二個參數為組態內容物件,型別為Object
- 第三個參數為區段的XML節點,型別為XmlNode
Imports System.Configuration
Public Class DepartmentHandler
Implements IConfigurationSectionHandler
Public Function Create(ByVal parent As Object, ByVal configContext As Object, ByVal section As System.Xml.XmlNode) As Object Implements System.Configuration.IConfigurationSectionHandler.Create
Dim dept As New Department
dept.Name = section.SelectSingleNode("Name").InnerText
dept.Manager = section.SelectSingleNode("Manager").InnerText
Return dept
End Function
End Class
4、必須在專案中的app.Config組態檔的區段中宣告Department型別
"Mod08_Section.DepartmentHandler,Mod08_Section"
專案名稱.處理Department類別名稱,專案名稱
5、取出自訂Section區段,使用Confurationmanager類別的GetSection方法取的Department區段再轉型為Department類別,即可使用存取
Imports System.Configuration
Dim dep As Department = CType(ConfigurationManager.GetSection("Department"), Department)
TextBox1.Text = dep.Name
TextBox2.Text = dep.Manager
沒有留言:
張貼留言