⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 编程之道VB.NETt程序设计入门-589M.zip
💻 VB
字号:
Imports System.Reflection


Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写处置以清理组件列表。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意:以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents treGetInfo As System.Windows.Forms.TreeView
    Friend WithEvents btnGetInfo As System.Windows.Forms.Button
    Friend WithEvents btnInvoke As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.treGetInfo = New System.Windows.Forms.TreeView()
        Me.btnGetInfo = New System.Windows.Forms.Button()
        Me.btnInvoke = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'treGetInfo
        '
        Me.treGetInfo.ImageIndex = -1
        Me.treGetInfo.Name = "treGetInfo"
        Me.treGetInfo.SelectedImageIndex = -1
        Me.treGetInfo.Size = New System.Drawing.Size(392, 272)
        Me.treGetInfo.TabIndex = 0
        '
        'btnGetInfo
        '
        Me.btnGetInfo.Location = New System.Drawing.Point(48, 288)
        Me.btnGetInfo.Name = "btnGetInfo"
        Me.btnGetInfo.Size = New System.Drawing.Size(80, 24)
        Me.btnGetInfo.TabIndex = 1
        Me.btnGetInfo.Text = "获取类信息"
        '
        'btnInvoke
        '
        Me.btnInvoke.Location = New System.Drawing.Point(248, 288)
        Me.btnInvoke.Name = "btnInvoke"
        Me.btnInvoke.Size = New System.Drawing.Size(80, 24)
        Me.btnInvoke.TabIndex = 2
        Me.btnInvoke.Text = "动态调用"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(392, 326)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnInvoke, Me.btnGetInfo, Me.treGetInfo})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetInfo.Click
        Dim sAssemblyPath = "C:\Windows\Microsoft.NET\Framework\v1.0.3705\System.dll"

        Dim objAssembly As [Assembly]

        Try
            objAssembly = [Assembly].LoadFrom(sAssemblyPath)

            Dim t As System.Type
            For Each t In objAssembly.GetTypes()
                Dim tnClass As New TreeNode(t.FullName)
                treGetInfo.Nodes.Add(tnClass)

                Dim tnCtors As New TreeNode("构造函数")
                Dim c As ConstructorInfo
                For Each c In t.GetConstructors()
                    tnCtors.Nodes.Add(c.ToString())
                Next
                tnClass.Nodes.Add(tnCtors)

                Dim tnProp As New TreeNode("属性")
                Dim p As PropertyInfo
                For Each p In t.GetProperties()
                    tnProp.Nodes.Add(p.ToString())
                Next
                tnClass.Nodes.Add(tnProp)

                Dim tnMethod As New TreeNode("方法")
                Dim m As MethodInfo
                For Each m In t.GetMethods()
                    tnMethod.Nodes.Add(m.ToString())
                Next
                tnClass.Nodes.Add(tnMethod)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnInvoke_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvoke.Click
        Dim t As Type
        t = Type.GetType("Case9_5.Class1", True)
        Dim newObj As Object
        newObj = Activator.CreateInstance(t)
        newObj.GetType().InvokeMember("Class1_Sub1", (BindingFlags.Default Or BindingFlags.InvokeMethod), Nothing, newObj, New Object() {""})
    End Sub
End Class

Public Class Class1
    Public Sub New()
        MessageBox.Show("动态构造Class1")
    End Sub

    Public Sub Class1_Sub1()
        MessageBox.Show("动态调用Class1_Sub1")
    End Sub
End Class


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -