VB.Net by API/System.Reflection/Assembly

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

Assembly.CodeBase

  
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


Assembly.EntryPoint

  
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


Assembly.FullName

  
Imports System.Reflection
Public Class Tester
    Public Shared Sub Main
    
        Dim myCurrentAppDomain As AppDomain
        Dim myAssemblies() As [Assembly]
        Dim myAssembly As [Assembly]
        Try
            myCurrentAppDomain = AppDomain.CurrentDomain
            myAssemblies = myCurrentAppDomain.GetAssemblies
            For Each myAssembly In myAssemblies
                Console.WriteLine(myAssembly.FullName)
            Next
            Console.WriteLine(myCurrentAppDomain.FriendlyName)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

    End Sub
End Class


Assembly.GetCustomAttributes

  
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


Assembly.GetExecutingAssembly()

  
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


Assembly.GetLoadedModules()

  

 
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


Assembly.GetManifestResourceNames()

  
 
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


Assembly.GetReferencedAssemblies()

  
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


Assembly.GetTypes()

  
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


Assembly.GlobalAssemblyCache

  
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


Assembly.Load

  
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


Assembly.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