獲得一個命名空間中所有的類等,並給類屬性付值。

來源:互聯網
上載者:User

以下只是獲得命名空間中的類,  If t.IsClass Then
其他的你可以判斷,  If t.IsValueType Then結構, If t.IsInterface介面
把這個命名空間中的類執行個體化:a.CreateInstance(t.FullName)
同樣你可以根據這個類,取得它其中有的屬性System.ComponentModel.TypeDescriptor.GetProperties(t)
也可以給屬性付值,也可以獲得事件:System.ComponentModel.TypeDescriptor.GetEvents(t),

其他的看看MSDN就知道了。

以下只作為參考:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable
        dt = GetClassName("webcontrol")

        For i As Integer = 0 To dt.Rows.Count - 1

            If dt.Rows(i)(0).ToString.ToLower = "textbox" Then
                Call SetClassPropertyValue(dt.Rows(i)(2), "text", "123")
            End If
        Next
    End Sub

    Private Function GetClassName(ByVal namespacestr As String) As DataTable
        Dim a As System.Reflection.Assembly
        Dim t As Type
        Dim dt As DataTable
        Dim dr As DataRow

        dt = New DataTable
        dt.Columns.Add(New DataColumn("ClassName"))
        dt.Columns.Add(New DataColumn("ClassFullName"))
        dt.Columns.Add(New DataColumn("InstanceClass"))

        a = System.Reflection.Assembly.Load(namespacestr)
        For Each t In a.GetExportedTypes

            If t.IsClass Then
                dr = dt.NewRow
                dr("ClassName") = t.Name
                dr("ClassFullName") = t.FullName
                dr("InstanceClass") = a.CreateInstance(t.FullName)
                dt.Rows.Add(dr)
                dt.AcceptChanges()
            End If
        Next
        Return dt
    End Function

    Private Sub SetClassPropertyValue(ByVal t As Type, ByVal propertyStr As String, ByVal value As String)
        Dim cc As System.Web.UI.ControlCollection
        Dim pd As System.ComponentModel.PropertyDescriptor
        Dim pdc As System.ComponentModel.PropertyDescriptorCollection

        pdc = System.ComponentModel.TypeDescriptor.GetProperties(t)
        pd = pdc.Find(propertyStr, False)
        pd.SetValue(t, value)
    End Sub

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.