Introduction to Reflection API

Assembly

Using Assembly you can define and load assemblies, load modules that are listed in the assembly manifest, and locate a type from this assembly and create an instance of it.

Assembly class represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application. Assemply type provides various properties and method for getting any kind of information about assembly, getting loaded assemblies and loading assemblies

Some Properties of Assembly class

public virtual string CodeBase { get; } //Gets the location of the assembly as specified originally, for example, in an AssemblyName object.

public virtual string FullName { get; } //Gets the display name of the assembly.

public virtual IEnumerable<TypeInfo> DefinedTypes { get; } //Gets a collection of the types defined in this assembly.

public virtual IEnumerable<Module> Modules { get; } //Gets a collection that contains the modules in this assembly.

Getting loaded Assemblies

get the assembly that contains the code that is currently executing
var assembly = System.Reflection.Assembly.GetAssembly(Type type);
 
get the process executable in the default application domain. In other application domains, this is the first executable that was executed by AppDomain.ExecuteAssembly method
var assembly = System.Reflection.Assembly.GetEntryAssembly();
 
get the assembly of the method that invoked the currently executing method
var assembly = System.Reflection.Assembly.GetCallingAssembly();

Loading assemblies

load an assembly given the long form of its name
var assembly = System.Replection.Assembly.Load(string assemblyString);
 
load the assembly with a common object file format (COFF)-based image containing an emitted assembly
var assembly = System.Replection.Assembly.Load(byte[] rawAssembly);

other properties and methods you can check on MSDN

Example

Get executing assembly and output its name and version:

using System; using System.Reflection; ...

 

Page 2 of 11