VB.Net/Development/Assembly

Материал из VB Эксперт
Перейти к: навигация, поиск

Add all Public Assembly Form Type

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms public class MainClass

  Shared Sub Main()
     Dim form1 As Form = New Form1
     Application.Run(form1)
  End Sub

End Class

Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
   End Sub
   "Form overrides dispose to clean up the component list.
   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
   Friend WithEvents treeView1 As System.Windows.Forms.TreeView
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.treeView1 = New System.Windows.Forms.TreeView()
       Me.SuspendLayout()
       "
       "treeView1
       "
       Me.treeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.treeView1.ImageIndex = -1
       Me.treeView1.Location = New System.Drawing.Point(8, 16)
       Me.treeView1.Name = "treeView1"
       Me.treeView1.SelectedImageIndex = -1
       Me.treeView1.Size = New System.Drawing.Size(448, 200)
       Me.treeView1.Sorted = True
       Me.treeView1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(467, 248)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeView1})
       Me.Name = "Form1"
       Me.Text = "View Direct Members of Objects"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Function StripType(ByVal s As String) As String
       Dim spacepos As Integer
       spacepos = InStr(s, " ")
       If spacepos > 0 Then Return Mid$(s, spacepos + 1)
       Return (s)
   End Function
   Private Sub Form1_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load
       Dim asm As [Assembly]
       Dim asmtypes() As Type
       Dim ThisType As Type
       asm = Reflection.Assembly.GetAssembly(GetType(System.Windows.Forms.Form))
       asmtypes = asm.GetTypes()
       For Each ThisType In asmtypes
           If ThisType.IsClass And ThisType.IsPublic Then
               Dim tn As New TreeNode(ThisType.Name)
               treeView1.Nodes.Add(tn)
           End If
       Next
   End Sub

End Class


 </source>


Find Members with Binding Flags

<source lang="vbnet"> Imports System Imports System.Reflection

Public Class MainClass

   Shared Sub Main(  )
        Dim t As Type = Type.GetType("System.Reflection.Assembly")
        Dim mbrInfoArray As MemberInfo(  ) = t.FindMembers( _
            MemberTypes.Method, _
            BindingFlags.Public Or _
            BindingFlags.Static Or _
            BindingFlags.NonPublic Or _
            BindingFlags.Instance Or _
            BindingFlags.DeclaredOnly, _
            Type.FilterName, "Get*")
        Dim inf As MemberInfo
        For Each inf In mbrInfoArray
            Console.WriteLine("{0} is a {1}", _
                inf, inf.MemberType)
        Next
   End Sub
 

End Class


 </source>


Get Assembly Member type: Property or Member

<source lang="vbnet"> Imports System Imports System.Reflection

Public Class MainClass

   Shared Sub Main(  )
        Dim t As Type = Type.GetType("System.Reflection.Assembly")
        Console.WriteLine("Single type is {0}", t)
        Dim mbrInfoArray As MemberInfo(  ) = t.GetMembers(  )
        Dim inf As MemberInfo
        For Each inf In mbrInfoArray
            Console.WriteLine("{0} is a {1}", _
                inf, inf.MemberType)
        Next
   End Sub
 

End Class


 </source>


Get Class Name in Current Assembly

<source lang="vbnet"> Imports System.Reflection Class A End Class Class B End Class Class C End Class Class D End Class

Public Class MainClass

   Public Shared Sub Main()
       Dim ThisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
       Dim TypeObj As Type
       For Each TypeObj In ThisAssembly.GetTypes()
           Console.WriteLine(TypeObj.Name)
       Next
   End Sub

End Class


 </source>


Get Executing Assembly

<source lang="vbnet"> Imports System.Reflection

Public Class MainClass

   Public Shared Sub Main()
       Dim ThisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
       Dim Attr As Attribute
       For Each Attr In ThisAssembly.GetCustomAttributes(False)
           Console.WriteLine(Attr)
       Next
   End Sub

End Class


 </source>


Get info for current AppDomain

<source lang="vbnet"> Imports System.Reflection Module Program

 Sub Main()
   Dim defaultAD As AppDomain = AppDomain.CurrentDomain()
   PrintAllAssembliesInAppDomain(defaultAD)
 End Sub
 Public Sub PrintAllAssembliesInAppDomain(ByVal ad As AppDomain)
   Dim loadedAssemblies As Assembly() = ad.GetAssemblies()
   Console.WriteLine(ad.FriendlyName)
   For Each a As Assembly In loadedAssemblies
     Console.WriteLine("-> Name: {0}", a.GetName.Name)
     Console.WriteLine("-> Version: {0}", a.GetName.Version)
   Next
 End Sub

End Module


 </source>


Get Information from Assembly

<source lang="vbnet"> Imports System Imports System.Windows.Forms Imports System.Reflection Imports System.Runtime.InteropServices <Assembly: AssemblyTitle("My Title")> <Assembly: AssemblyDescription("A description.")> <Assembly: AssemblyCompany("Pretty Good Software")> <Assembly: AssemblyProduct("A Really Great Product!")> <Assembly: AssemblyCopyright("Copyright ? 2005")> <Assembly: AssemblyTrademark("TM")> <Assembly: CLSCompliant(True)> <Assembly: ComVisible(False)> "The following GUID is for the ID of the typelib if this project is exposed to COM <Assembly: Guid("4db4b206-3cc5-4ea0-9e91-59537aba04a8")> " Version information for an assembly consists of the following four values: " " Major Version " Minor Version " Build Number " Revision " " You can specify all the values or you can default the Build and Revision Numbers " by using the "*" as shown below: " <Assembly: AssemblyVersion("1.0.*")> <Assembly: AssemblyVersion("1.2.3.4")> <Assembly: AssemblyFileVersion("5.6.7.8")>

Public Class MainClass

  Shared Sub Main()
      Console.WriteLine( Application.ProductName )
      Console.WriteLine( Application.rupanyName )
      Console.WriteLine( Application.ProductVersion )
  End Sub 

End Class


 </source>


Hook into DomainUnload event

<source lang="vbnet"> Imports System.Reflection Module Program

   Sub Main()
       Dim anotherAD As AppDomain = AppDomain.CreateDomain("SecondAppDomain")
       " Load CarLibrary.dll into this new appdomain.
       anotherAD.Load("CarLibrary")
       " Hook into DomainUnload event.
       AddHandler anotherAD.DomainUnload, AddressOf anotherAD_DomainUnload
       " Now unload anotherAD.
       AppDomain.Unload(anotherAD)
   End Sub
   Public Sub PrintAllAssembliesInAppDomain(ByVal ad As AppDomain)
       Dim loadedAssemblies As Assembly() = ad.GetAssemblies()
       Console.WriteLine(ad.FriendlyName)
       For Each a As Assembly In loadedAssemblies
           Console.WriteLine("-> Name: {0}", a.GetName.Name)
           Console.WriteLine("-> Version: {0}", a.GetName.Version)
       Next
   End Sub
   Sub anotherAD_DomainUnload(ByVal sender As Object, ByVal e As EventArgs)
       Console.WriteLine("***** Unloaded anotherAD! *****")
   End Sub
   Sub defaultAD_ProcessExit(ByVal sender As Object, ByVal e As EventArgs)
       Console.WriteLine("***** Unloaded defaultAD! *****")
   End Sub

End Module


 </source>


Load all Types in one Assembly

<source lang="vbnet"> Imports System Imports System.Reflection

Public Class MainClass

   Shared Sub Main(  )
        Dim a As [Assembly] = [Assembly].Load("Mscorlib.dll")
        Dim theTypes As Type(  ) = a.GetTypes(  )
        Dim t As Type
        For Each t In theTypes
            Console.WriteLine("Type is {0}", t)
        Next t
        Console.WriteLine("{0} types found", theTypes.Length)
   End Sub
 

End Class


 </source>


Programmatically make a new app domain

<source lang="vbnet"> Imports System.Reflection Module Program

 Sub Main()
   Dim anotherAD As AppDomain = AppDomain.CreateDomain("SecondAppDomain")
   " Load CarLibrary.dll into this new appdomain.
   anotherAD.Load("CarLibrary")
   PrintAllAssembliesInAppDomain(anotherAD)
 End Sub
 Public Sub PrintAllAssembliesInAppDomain(ByVal ad As AppDomain)
   Dim loadedAssemblies As Assembly() = ad.GetAssemblies()
   Console.WriteLine(ad.FriendlyName)
   For Each a As Assembly In loadedAssemblies
     Console.WriteLine("-> Name: {0}", a.GetName.Name)
     Console.WriteLine("-> Version: {0}", a.GetName.Version)
   Next
 End Sub

End Module


 </source>


View all Assembly Form Type and its members in a Tree

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms public class MainClass

  Shared Sub Main()
     Dim form1 As Form = New Form1
     Application.Run(form1)
  End Sub

End Class

Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
   End Sub
   "Form overrides dispose to clean up the component list.
   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
   Friend WithEvents treeView1 As System.Windows.Forms.TreeView
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.treeView1 = New System.Windows.Forms.TreeView()
       Me.SuspendLayout()
       "
       "treeView1
       "
       Me.treeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.treeView1.ImageIndex = -1
       Me.treeView1.Location = New System.Drawing.Point(8, 16)
       Me.treeView1.Name = "treeView1"
       Me.treeView1.SelectedImageIndex = -1
       Me.treeView1.Size = New System.Drawing.Size(448, 200)
       Me.treeView1.Sorted = True
       Me.treeView1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(467, 248)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeView1})
       Me.Name = "Form1"
       Me.Text = "View Direct Members of Objects"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Function StripType(ByVal s As String) As String
       Dim spacepos As Integer
       spacepos = InStr(s, " ")
       If spacepos > 0 Then Return Mid$(s, spacepos + 1)
       Return (s)
   End Function
   Private Sub Form1_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load
       Dim asm As [Assembly]
       Dim asmtypes() As Type
       Dim ThisType As Type
       asm = Reflection.Assembly.GetAssembly(GetType(System.Windows.Forms.Form))
       asmtypes = asm.GetTypes()
       For Each ThisType In asmtypes
           If ThisType.IsClass Then
               Dim tn As New TreeNode(ThisType.Name)
               Dim members(), mi As MemberInfo
               treeView1.Nodes.Add(tn)
               members = ThisType.GetMembers(BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.Static)
               For Each mi In members
                   Dim methinfo As MethodInfo
                   Select Case mi.MemberType
                       Case MemberTypes.Method
                           methinfo = CType(mi, MethodInfo)
                           If Not methinfo.IsSpecialName Then
                               tn.Nodes.Add(StripType(mi.ToString))
                           End If
                       Case MemberTypes.Event
                           tn.Nodes.Add(StripType(mi.ToString) & " event")
                       Case Else
                           tn.Nodes.Add(StripType(mi.ToString))
                   End Select
               Next
           End If
       Next
   End Sub

End Class


 </source>


View all Assembly Form Type in a Tree

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms public class MainClass

  Shared Sub Main()
     Dim form1 As Form = New Form1
     Application.Run(form1)
  End Sub

End Class

Public Class Form1

   Inherits System.Windows.Forms.Form
  1. Region " Windows Form Designer generated code "
   Public Sub New()
       MyBase.New()
       "This call is required by the Windows Form Designer.
       InitializeComponent()
   End Sub
   "Form overrides dispose to clean up the component list.
   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
   Friend WithEvents treeView1 As System.Windows.Forms.TreeView
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.IContainer
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.treeView1 = New System.Windows.Forms.TreeView()
       Me.SuspendLayout()
       "
       "treeView1
       "
       Me.treeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                   Or System.Windows.Forms.AnchorStyles.Left) _
                   Or System.Windows.Forms.AnchorStyles.Right)
       Me.treeView1.ImageIndex = -1
       Me.treeView1.Location = New System.Drawing.Point(8, 16)
       Me.treeView1.Name = "treeView1"
       Me.treeView1.SelectedImageIndex = -1
       Me.treeView1.Size = New System.Drawing.Size(448, 200)
       Me.treeView1.Sorted = True
       Me.treeView1.TabIndex = 0
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(467, 248)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeView1})
       Me.Name = "Form1"
       Me.Text = "View Direct Members of Objects"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Function StripType(ByVal s As String) As String
       Dim spacepos As Integer
       spacepos = InStr(s, " ")
       If spacepos > 0 Then Return Mid$(s, spacepos + 1)
       Return (s)
   End Function
   Private Sub Form1_Load(ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles Me.Load
       Dim asm As [Assembly]
       Dim asmtypes() As Type
       Dim ThisType As Type
       asm = Reflection.Assembly.GetAssembly(GetType(System.Windows.Forms.Form))
       asmtypes = asm.GetTypes()
       For Each ThisType In asmtypes
           If ThisType.IsClass Then
               Dim tn As New TreeNode(ThisType.Name)
               treeView1.Nodes.Add(tn)
           End If
       Next
   End Sub

End Class


 </source>