BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Reflecting Assembly Information

The seventeenth part of the Reflection tutorial looks at how information about assemblies can be obtained using reflection. This is an important prerequisite for more advanced reflection techniques, such as dynamically loading assemblies at run time.

Assemblies

Assemblies are the building blocks of all software that runs using the .NET framework. Assemblies are files, such as dynamic linked libraries (DLLs) and executables (EXEs) that contain programs, library code, controls and resources that are used at run time. When using reflection, you can create an Assembly object that represents an assembly and examine its properties to find out more information about it. You can also use the object's methods to obtain Type objects for each of the classes, structures and other types that are contained within.

In this article we'll look at some of the key methods for obtaining an Assembly object, some of the properties that provide general information about the underlying assembly, and the methods that can be used to extract the file's contained types. If you wish to try out the samples, create a new console application named, "AssemblyReflection", and try running the example code within the Main method. Also include the following using directive, as we'll be using types from the System.Reflection namespace:

using System.Reflection;

Obtaining Assembly Instances

There are a number of ways in which you can create an Assembly instance. The method that you choose depends upon the information that you have beforehand and whether the assembly is already loaded or not. The following sections show several common options, primarily using static methods from the Assembly class.

Getting the Assembly that Contains a Given Type

If you have a Type object that represents any type, you can obtain details of the assembly that it is contained within. You do this by executing the GetAssembly method of the Assembly class, or by reading the Type's Assembly property. Both options return an Assembly object.

Type type = typeof(Program);
Assembly asm = Assembly.GetAssembly(type);
Assembly asm2 = type.Assembly;

Console.WriteLine(asm.FullName);
Console.WriteLine(asm2.FullName);

/* OUTPUT

AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

*/

Getting the Current Assembly

If you want to examine the assembly that contains the code that is currently running, you can use the GetExecutingAssembly method, as shown below:

Assembly asm = Assembly.GetExecutingAssembly();
Console.WriteLine(asm.FullName);

/* OUTPUT

AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

*/

Getting the Calling Assembly

If you are creating a library method that uses reflection to obtain details of an executing assembly, you cannot use GetExecutingAssembly, as that would always retrieve details about the assembly containing the library code. In these situations you need to obtain an Assembly object for the assembly that called the library method. You can get such an object using the GetCallingAssembly method.

The sample below shows the syntax for the method. As only one assembly is present in the project, only this assembly can be retrieved.

Assembly asm = Assembly.GetCallingAssembly();
Console.WriteLine(asm.FullName);

/* OUTPUT

AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

*/

Loading an Assembly by Name

When you know the full name for an assembly, you can load its details using the Load method. This method uses probing to find the assembly that you specify, looking for it in the application folder and other folders, as described in the ".NET Assemblies" article.

The following code shows a simple version of the method, which accepts the assembly name using a string parameter. Other overloads are available but are not described in this article.

Assembly asm = Assembly.Load("
    AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
Console.WriteLine(asm.FullName);

/* OUTPUT

AssemblyReflection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

*/

NB: If the probing finds an assembly with the correct name but incorrect version, an exception is thrown.

Loading Assemblies from Disk

Another option for reflecting assembly information is using the LoadFrom method. You provide this method with the file system path of the assembly to be loaded. It is useful when you want to load an assembly at runtime, perhaps providing the name of the target file using a configuration file or from user input.

The code below shows an example of how the method can be called.

Assembly asm = Assembly.LoadFrom(@"C:\Test\MyAssembly.dll");
10 July 2012