VB.Net Tutorial/Development/Assembly — различия между версиями

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

Текущая версия на 12:53, 26 мая 2010

Get Assembly: Code base, Full name, Entry point, Assembly Cache, Location

Imports System.Reflection
Module Module1
    Sub Main()
        Dim thisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
        Console.WriteLine("Code base: " & thisAssembly.CodeBase)
        Console.WriteLine("Full name: " & thisAssembly.FullName)
        Console.WriteLine("Entry point: " & thisAssembly.EntryPoint.ToString())
        Console.WriteLine("From Global Assembly Cache: " & thisAssembly.GlobalAssemblyCache)
        Console.WriteLine("Location: " & thisAssembly.Location)
    End Sub
End Module
Code base: file:///C:/Java_Dev/WEB/dev/VB/main.exe
Full name: main, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Entry point: Void Main()
From Global Assembly Cache: False
Location: C:\Java_Dev\WEB\dev\VB\main.exe

Get Referenced Assemblies

Imports System.Reflection
Module Module1
    Sub Main()
        Dim thisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
        Console.WriteLine("Referenced Assemblies: ")
        Dim RefAssembly As AssemblyName
        For Each RefAssembly In thisAssembly.GetReferencedAssemblies()
            Console.WriteLine(RefAssembly.FullName)
        Next
    End Sub
End Module
Referenced Assemblies:
mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Get types from Assembly

Imports System.Reflection
Module Module1
    Sub Main()
        Dim thisAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
        Console.Write("Types: ")
        Dim TypeObj As Type
        For Each TypeObj In thisAssembly.GetTypes()
            Console.Write(TypeObj.Name & " ")
        Next
    End Sub
End Module
Types: MyApplication MyComputer MyProject MyWebServices ThreadSafeObjectProvider`1 Module1 "

Given an assembly, display details from its manifest

Imports System.Text
Imports System.Security.Cryptography
Public Class Tester
    Public Shared Sub Main
        Dim useAssembly As System.Reflection.Assembly
        Try
            useAssembly = Reflection.Assembly.LoadFile("YourAssemblyLocation")
        Catch ex As System.Exception
            Console.WriteLine(ex.Message)
            Return
        End Try

        Console.WriteLine("Full Name: " & useAssembly.FullName)
        Console.WriteLine("Resources")
        For Each oneName As String In useAssembly.GetManifestResourceNames()
            Console.WriteLine("   - " & oneName)
        Next oneName
        Console.WriteLine("Exported Types")
        For Each oneType As System.Type In useAssembly.GetExportedTypes()
            Console.WriteLine("   - " & oneType.Name)
        Next oneType
        Console.WriteLine("Modules")
        For Each oneModule As Reflection.Module In useAssembly.GetLoadedModules()
            Console.WriteLine("   - " & oneModule.Name)
            For Each oneType As System.Type In oneModule.GetTypes()
                Console.WriteLine("     Type: " & oneType.Name)
                For Each oneField As Reflection.FieldInfo In oneType.GetFields()
                    Console.WriteLine("        Field: " & oneField.ToString())
                Next oneField
                For Each oneMethod As Reflection.MethodInfo In oneType.GetMethods()
                    Console.WriteLine("        Method: " & oneMethod.ToString())
                Next oneMethod
            Next oneType
        Next oneModule
    End Sub
End Class
Absolute path information is required.

Load Assembly from a url

imports System.Reflection              " necessary for instantiating assemblies
Imports System.Windows.Forms
Public Class Tester
    Public Shared Sub Main
               try
                       dim strUrl as String = "http" & "://localhost/MultiFileAssyVB/vbDeploy.exe"
"                      dim strUrl as String = "http" & "://localhost/MultiFileAssy/csDeploy.exe"
                       dim a as [Assembly] = [Assembly].LoadFrom(strUrl)
                       
                       dim t as Type = a.GetType("Form1")
"                      dim t as Type = a.GetType("csDeploy.Form1")
                       
                       dim o as Object = Activator.CreateInstance(t)
                       
                       dim frm as Form = CType(o, Form)
                       
                       frm.Show()
               
               catch ex as Exception
                       Console.WriteLine(ex.ToString)
               End Try         
    End Sub
End Class

Load Assembly from DLL

Option Strict On
Imports System.IO
Imports System.Reflection
Public Module Reflection
   Public Sub Main
      Const path As String = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll"
      If File.Exists(path) Then
         Dim ass As Assembly = Assembly.LoadFrom(path)
         Dim typ As Type = ass.GetType("System.Array")
         Dim members As MemberInfo() = typ.GetMembers(BindingFlags.Instance _
                                       Or BindingFlags.Public)
         Console.WriteLine(members.Length)
      End If
   End Sub
End Module
43