Reflection with C#

Assembly is more than we think of it. Even it looks just one file; it contains several pieces of information.
  • Assembly metadata – it is also called manifest and it includes such items as name, strong name, version, and culture information.
  • Type metadata – contains such information as namespace, class and method names, properties and constructs with parameters information.
  • Code – it is actually Intermediate Language that is compiled info Assembly code by our compiler.
  • Resources – this are objects used within the code, string, images and files.
If we want to examine Assembly, we’ll have to use Assembly class with the following methods.

 

NameDescription
GetAssembly Assembly that contains a specified type
GetCallingAssembly Assembly that contains the code
GetEntryAssembly Assembly that contains the code that started up the current process
GetExecutingAssembly Assembly that contains the currently executing code
Load Assembly into the current AppDomain
LoadFile Assembly by specifying the path
LoadFrom Assembly into the current AppDomain
ReflectionOnlyLoad Assembly, but allows only interrogation
ReflectionOnlyLoadFrom Assembly located at a specific path

We can create assembly object just like that:

Assembly theAssembly = Assembly.GetExecutingAssembly();

After we have an instance of the Assembly we can interrogate it for the information about Assembly.

Here is a list of available properties.

 

NameDescription
EntryPoint First code to be executed
FullName Fully qualified name
GlobalAssemblyCache Checks if assembly was loaded from the GAC
Location Path to the assembly
ReflectionOnly Checks if assembly was loaded for reflection

Methods

 

NameDescription
CreateInstance Creates an instance of a specified type
GetCustomAttributes Gets an array of attributes
GetExportedTypes Gets a collection of types visible outside the assembly
GetFile Gets a FileStream object for a file contained resources
GetFiles Gets an array of FileStream files
GetLoadedModules Gets an array of the currently loaded modules
GetModule Gets a specified module
GetModules Gets all the modules from the assembly
GetName Gets an AssemblyName
GetSatelliteAssembly Gets a satellite assembly
GetTypes Gets an array of all the types
IsDefined Gets a value of a specific attribute

Assembly myAssembly = Assembly.GetExecutingAssembly();
Console.WriteLine("Full Name: {0}", myAssembly.FullName);
Console.WriteLine("Location: {0}", myAssembly.Location);
Console.WriteLine("Only Reflection?: {0}", myAssembly.ReflectionOnly);

In order to lower overhead with instantiating Assembly we can load it for the Reflection only with the help of these methods ReflectionOnlyLoad and ReflectionOnlyLoadFrom.

string fullName = "System.Transactions, Version=2.0.0.0, " +
    "Culture=neutral, PublicKeyToken=b77a5c561934e089";
Assembly theMyAssembly = Assembly.ReflectionOnlyLoad(fName);
Console.Write("Location: {0}", theMyAssembly.Location);

Each Assembly has several modules which have their own methods and properties.

Properties

 

NameDescription
Assembly Provides the assembly that this module resides
FullyQualifiedName Gets the full name
Name Gets the name of the module

Methods

 

NameDescription
FindTypes Gets types matching certain criteria
GetCustomAttributes Gets the attributes
GetField Gets a specific
GetFields Gets all the fields
GetMethod Gets a specific method
GetMethods Gets all the methods
GetTypes Gets all the types
IsResource Checks if objects are resources