This article shows getting the information about an assembly. Another article shows getting the class information:
Using Reflection in .NET: Getting the Class Information
Namespaces for Reflection
The primary namespace for the reflection functionality is in the System.Reflection namespace. This namespace contains the Assembly class that can be used to load an assembly and then get information about various pieces of an assembly from there. This namespace also contains classes that hold information about individual types in an assembly. They include: ConstructorInfo, EventInfo, MethodInfo, ParameterInfo, and so on.
However, all the type information comes from the Type class. This class can hold not just classes, but also interfaces, enumerations, value types, etc. This class is contains methods to obtain additional information about the pieces that a type might contain. For example, GetMethods() returns an array of MethodInfo objects – as you can expect contains information about individual methods in a class (more discussion in this article: Using Reflection in .NET – Getting the Class Information).
Getting the Assembly Information
The utility shown in Figure 1 gets the details of an assembly.
Figure 1. The assembly details via reflection
The Code Listing 1 shows the entire code behind the ‘Get Assembly Details’ button. Discussion of the code follows after that.
Code Listing 1. Getting the assembly details
/// <summary>
/// Get the details of the assembly
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAssembly_Click(object sender, EventArgs e)
{
// Load the Assembly
try
{
Assembly assembly = Assembly.LoadFile(txtAssembly.Text);
StringBuilder details = new StringBuilder();
// Get the basic Details
details.AppendLine("Assembly Name: " + assembly.FullName + Environment.NewLine);
details.AppendLine("Location: " + assembly.Location + Environment.NewLine);
// Modules
details.AppendLine("Modules:");
Module[] modules = assembly.GetModules();
foreach (Module module in modules)
details.AppendLine(module.Name);
// Types
Type[] types = assembly.GetTypes();
// Classes
details.AppendLine(Environment.NewLine + "Classes:");
foreach (Type type in types)
if ((type.IsClass) && (type.IsPublic))
details.AppendLine(type.FullName);
// Interfaces
details.AppendLine(Environment.NewLine + "Interfaces:");
foreach (Type type in types)
if (type.IsInterface)
details.AppendLine(type.FullName);
// Structures
details.AppendLine(Environment.NewLine + "Structures: ");
foreach (Type type in types)
if (type.IsValueType)
details.AppendLine(type.FullName);
// Enumerations
details.AppendLine(Environment.NewLine + "Enumerations:");
foreach (Type type in types)
if ((type.IsEnum) && (type.IsPublic))
details.AppendLine(type.FullName);
txtDetails.Text = details.ToString();
}
catch (Exception)
{
txtDetails.Text = "Assembly could Not be loaded.";
}
}
Code Discussion
Loading the Assembly
Assembly assembly = Assembly.LoadFile(txtAssembly.Text);
Here I am loading an assembly from the text entered by the user in the txtAssembly textbox. I used a static method in the Assembly class called LoadFile, which takes a managed dll or exe as the parameter. The Assembly class provides several methods for loading an assembly and then examine/manipulate the assembly. These methods are Load(), LoadFile(), LoadFrom(), and LoadModule(). Each of these methods comes with several signatures.
Basic Details
// Get the basic Details
details.AppendLine("Assembly Name: " + assembly.FullName + Environment.NewLine);
details.AppendLine("Location: " + assembly.Location + Environment.NewLine);
The Assembly class itself comes with several properties for obtaining the basic information about an assembly. Here we obtained the full name and location of an assembly via the properties.
Getting Types
// Types
Type[] types = assembly.GetTypes();
An assembly is full of types – classes, interfaces, enumerations, and so on. A huge array of types (Type[]) can be obtained from the GetTypes() method. The Type class provides properties like IsClass, IsInterface, etc. to shed more information about the individual code. As shown in the code, you can loop through the Type[] array and separate out the classes, interfaces, and so on.