Logo
programming4us
programming4us
programming4us
programming4us
Windows XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
 
programming4us
Windows 7

Visual Basic 2010 : Reflection - Understanding Assemblies Metadata & Getting Assembly Information

6/18/2011 4:23:41 PM
There are situations in which you need to implement logic for performing some tasks depending on user choices. This kind of a situation is not uncommon. The problem is when you cannot predetermine the code required for executing actions depending on user input. Think of code generators: Such tools know how to generate code but cannot predetermine what code has to be generated until the users specify their requirements. Also think of assemblies external from your application. In some cases you might want to use types from an external assembly; in other cases you might just want to get information on types provided by the assembly; and in other cases you might want to reach members with limited scope visibility that you could not reach by simply adding a reference. Reflection is a key part in the .NET Framework that enables accomplishing all these mentioned scenarios.

Introducing Reflection

Reflection is an important part of the .NET Framework that provides the ability for interrogating assemblies’ metadata and collecting information on types exposed by assemblies. Reflection also enables invoking code from external assemblies and generating code on-the-fly. You can take advantage of Reflection by using objects exposed by the System.Reflection namespace. It can be particularly useful when you need to generate code according to some user input or when you are in late bound scenarios where making decisions on what code must be invoked (or generated) is something determined at runtime. Before putting your hands on code, it is necessary to get an overview of how assemblies are structured so that you can have a better understanding of what kind of information you can investigate with the Reflection.

Understanding Assemblies’ Metadata

As you know, when you build an executable with Visual Basic, you build a .NET assembly. An assembly is basically a container of metadata and code. Metadata is information that the CLR uses in correctly loading and running the assembly. Figure 1 represents how an assembly is structured.

Figure 1. How an assembly is structured.


The Assembly Metadata, also known as assembly manifest, basically provides assembly information such as the name, version, culture, copyright information, and signature. The Type Metadata contains information on types defined within the assembly, such as class names and names of class members, including their parameters. The Code part is the actual Intermediate Language code that will be executed when the assembly is loaded. The Resources block contains all resources required by the assembly, such as images, icons, and strings. Also notice that types within an assembly can be grouped into multiple modules. A module is a container of types whereas an assembly is a container of modules. With Reflection you can inspect metadata and code from an assembly using Visual Basic code, including assembly information.

Note

When talking about assemblies, we usually refer to single file executables. Assemblies can be composed of multiple linked files; keep in mind that assembly metadata needs to reside only in the main assembly. This is a special case and cannot be accomplished with Visual Studio (you should use manually MSBuild), but it is something that it is worth mentioning.


Preparing a Sample Assembly

Before showing Reflection capabilities, a good idea is to prepare an appropriate code example. First, create a new class library project and name it People. The goal of the library is to expose a special implementation of the Person class, with interfaces and enumerations implementations for a better demonstration on Reflection. When ready, write the code in Listing 1, which is quite simple.

Listing 1. Preparing Code for Reflection
Imports System.Text


Public Enum Genders
Male = 0
Female = 1
End Enum

Public Interface IPerson
Property FirstName As String
Property LastName As String
Property Age As Integer
Property Gender As Genders
Event InstanceCreated()
Function BuildFullName() As String
End Interface

Public Class Person
Implements IPerson

Public Property FirstName As String Implements IPerson.FirstName
Public Property Gender As Genders Implements IPerson.Gender
Public Property LastName As String Implements IPerson.LastName
Public Property Age As Integer Implements IPerson.Age
Public Event InstanceCreated() Implements IPerson.InstanceCreated

Public Overridable Function BuildFullName() As String _
Implements IPerson.BuildFullName
Dim fullName As New StringBuilder
fullName.Append(LastName)
fullName.Append(" ")
fullName.Append(FirstName)
fullName.Append(", ")
fullName.Append(Gender.ToString)
fullName.Append(", of age ")
fullName.Append(Age.ToString)

Return fullName.ToString
End Function
End Class


Build the project; then add a new Console project to the current solution. Finally add a reference to the People class library so that, just for demo purposes, you can load the assembly for Reflection without specifying the full path.

Getting Assembly Information

You get assembly metadata information creating an instance of the System.Reflection.Assembly class. This class provides both static and instance members for accessing assembly information. Typically you use one of the methods summarized in Table 1 to load an assembly for getting information.

Table 1. Methods for Loading an Assembly
MethodDescription
GetAssemblyLoads an assembly containing the specified type
GetCallingAssemblyGets the assembly that stores the code that invoked the current method
GetExecutingAssemblyReturns the instance of the current assembly
GetEntryAssemblyReturns the instance of the assembly that ran the current process
LoadLoads the specified assembly into the current application domain
LoadFileLoads the specified assembly from the specified path
LoadFromLoads the specified assembly into the current application domain, given the specified path
ReflectionOnlyLoadLike Load, but allows only Reflection inspection and not code execution
ReflectionOnlyLoadFromLike LoadFrom, but allows only Reflection inspection and not code execution

When you get the instance of the assembly you want to inspect, you can access information via some useful properties. The code in Listing 2 shows how to accomplish this. (See comments for explanations.)

Listing 2. Inspecting Assembly Information
Imports System.Reflection


Module GettingAsmInfo

Sub Main()

'Infers System.Reflection.Assembly
Dim asm = Assembly.ReflectionOnlyLoadFrom("People.dll")

With asm
'Gets the full assembly name with
'version and culture
Console.WriteLine("Assembly name:")
Console.WriteLine(.FullName)
'Gets whether the assembly is fully trusted
Console.WriteLine("Is full-trust: {0}", .IsFullyTrusted)
'Gets the assembly entry point. If empty, the
'constructor is the entry point
Console.WriteLine("The entry point method is: {0}", .EntryPoint)
'Gets the .NET version that the
'assembly was built upon
Console.WriteLine("Image runtime version: {0}", .ImageRuntimeVersion)
'Gets whether the assembly was loaded from
'the GAC
Console.WriteLine("Loaded from the GAC: {0}", .GlobalAssemblyCache)
'Gets the assembly location
Console.WriteLine("Assembly path: {0}", .Location)

'Gets an array of modules loaded
'by the assembly
Console.WriteLine("Loaded modules: ")
For Each item As System.Reflection.Module _
In .GetLoadedModules
Console.WriteLine(" {0}", item.Name)
Next
End With
Console.ReadLine()
End Sub
End Module


Notice how the code uses the ReflectionOnlyLoadFrom method to enable only inspection without code execution capabilities. If you run the preceding code, you get the following result:

Assembly name:
People, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Is full-trust: True
The entry point method is:
Image runtime version: v4.0.21006
Loaded from the GAC: False
Assembly path: C:\Users\Alessandro\documents\visual studio
2010\Projects\Reflection\Reflection\bin\Debug\People.dll
Loaded modules:
People.dll

Notice that the RTM version number of the .NET Framework 4 has a build number different than the previous one. The Assembly.GetModules method returns an array of modules loaded by the instance of the assembly. Other interesting methods are GetExportedTypes, which return an array of publicly visible types, and GetFiles, which returns an array of FileStream objects, each representing a file in the assembly’s resources. Inspecting assembly information is just the first level of Reflection. The next step is inspecting types.

Other -----------------
- Visual Basic 2010 : Hosting WCF Services in Internet Information Services & Configuring Services with the Configuration Editor
- Supporting Mobile Windows 7 Users : Understanding DirectAccess & Using BranchCache
- Microsoft Excel 2010 - Creating and Modifying Charts - Changing Chart Titles
- Microsoft Excel 2010 - Creating and Modifying Charts - Changing Chart Gridlines and Axes
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Working with SharePoint Workspaces
- Microsoft PowerPoint 2010 : Working Together on Office Documents - Collaborating with Documents on Windows Live
- Microsoft Visio 2010 : Starting a New Diagram from a Sample Diagram
- Microsoft Visio 2010 : Creating a New Diagram - Using AutoAdd and AutoDelete
- Microsoft Visio 2010 : Creating a New Diagram - Using AutoConnect and Quick Shapes
- Troubleshooting Remote Access Issues (part 3) - Troubleshooting a VPN Client & A Few Words about Teredo
 
 
Video tutorials
- How To Install Windows 8 On VMware Workstation 9

- How To Install Windows 8

- How To Install Windows Server 2012

- How To Disable Windows 8 Metro UI

- How To Change Account Picture In Windows 8

- How To Unlock Administrator Account in Windows 8

- How To Restart, Log Off And Shutdown Windows 8

- How To Login To Skype Using A Microsoft Account

- How To Enable Aero Glass Effect In Windows 8

- How To Disable Windows Update in Windows 8

- How To Disable Windows 8 Metro UI

- How To Add Widgets To Windows 8 Lock Screen
programming4us programming4us
Popular tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 windows Phone 7 windows Phone 8
programming4us programming4us
 
Popular keywords
HOW TO Swimlane in Visio Visio sort key Pen and Touch Creating groups in Windows Server Raid in Windows Server Exchange 2010 maintenance Exchange server mail enabled groups Debugging Tools Collaborating
programming4us programming4us
 
programming4us
Girls
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone