Recipe 14.32. Reading an Assembly s Details


Recipe 14.32. Reading an Assembly's Details

Problem

You're curious about the contents of an assembly, and it's not because you want to find out its secrets.

Solution

Sample code folder: Chapter 14\AssemblyManifest

Use the classes of the System.Reflection namespace to access the contents of any assembly.

Discussion

This recipe's sample code displays some basic information contained within an assembly. Create a new Windows Forms application, and add the following controls to Form1:

  • A TextBox control named AssemblyLocation.

  • A Button control named ReadAssembly. Set its Text property to Show.

  • A TextBox control named AssemblyDetail. Set its Multiline property to true and its ScrollBars property to Both. Also set its WordWrap property to False. Size this control to fill much of the form, as it will display a lot of content.

The form should look like the one in Figure 14-31.

Now, add the following code to the form's code template:

 Private Sub ReadAssembly_Click( _       ByVal sender As System.Object, _       ByVal e As System.EventArgs) _       Handles ReadAssembly.Click    ' ----- Given an assembly, display details from its    '       manifest.    Dim useAssembly As System.Reflection.Assembly    Dim displayContent As New System.Text.StringBuilder 

Figure 14-31. The controls on the show assembly details sample


    ' ----- Load this assembly.    If (My.Computer.FileSystem.FileExists( _          AssemblyLocation.Text) = False) Then       MsgBox("Please supply a valid assembly file name " & _          "with a valid path.")       Return    End If    Try       useAssembly = Reflection.Assembly.LoadFile( _          AssemblyLocation.Text)    Catch ex As System.Exception       MsgBox("Could not access the assembly: " & ex.Message)       Return    End Try    ' ----- Clear the existing content.    AssemblyDetail.Clear( )    ' ----- Show its full complex name.    displayContent.AppendLine("Full Name: " & _       useAssembly.FullName)    ' ----- List all of the resources.    displayContent.AppendLine( )    displayContent.AppendLine("Resources")    For Each oneName As String In _          useAssembly.GetManifestResourceNames( )        displayContent.AppendLine(" - " & oneName)    Next oneName    ' ----- List all of the exported types.    displayContent.AppendLine( )    displayContent.AppendLine("Exported Types")    For Each oneType As System.Type In _            useAssembly.GetExportedTypes( )       displayContent.AppendLine(" - " & oneType.Name)    Next oneType    ' ----- Process each module, and each type within    '       the module.    displayContent.AppendLine( )    displayContent.AppendLine("Modules")    For Each oneModule As Reflection.Module In _          useAssembly.GetLoadedModules( )       displayContent.AppendLine(" - " & oneModule.Name)       For Each oneType As System.Type In oneModule.GetTypes( )          ' ----- These types will be the primary          '       classes/forms in the assembly.          displayContent.AppendLine(" Type: " & _             oneType.Name)          ' ----- Show the fields included in each type.          For Each oneField As Reflection.FieldInfo In _                oneType.GetFields( )             displayContent.AppendLine("        Field: " & _                oneField.ToString( ))          Next oneField          ' ----- Show the methods included in each type.          For Each oneMethod As Reflection.MethodInfo In _                oneType.GetMethods( )             displayContent.AppendLine(" Method: " & _                oneMethod.ToString( ))          Next oneMethod       Next oneType    Next oneModule    ' ----- Display the results.    AssemblyDetail.Text = displayContent.ToString( ) End Sub 

To use the program, type a valid assembly file path into the AssemblyLocation field, and then click the Show button. The AssemblyDetail text box will be filled with details from the specified assembly. For Windows Forms assemblies, you will be amazed at the amount of content contained in even the simplest program. Figure 14-32 shows this program used on itself.

Figure 14-32. The assembly details for an application assembly


The .NET Framework includes a system called reflection that lets you examine every aspect of an assembly, if you have the proper security rights. You can view the basic assembly details, such as the version number and copyright name. You can also examine all classes, class methods, method parameters, and even the Intermediate Language (IL) code within a method. It's all available through the System.Reflection namespace.

The code shown here uses only a small portion of the available reflection features. The Reflection.Module class, for example, has many properties and methods that fully describe a module, which is typically an EXE or DLL file.

This sample code does not take into account nested types. Any class can include subordinate class definitions. To access these from a System.Type instance, use that instance's GetNestedTypes() method.





Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net