C# Reflection - What is Assembly?

C#  C#  reflection 

Assembly is the main unit in .NET applications. Assembly is a collection of types and its information. There are two types of assemblies:

  1. Executable files (EXE)
  2. Libraries (DLL)

Both type of assemblies includes information about types and its members in its metadata.

There are three ways to get Assembly instances:

  1. Geting assembly instance from type information
  2. Getting assembly instance from current assembly and parent assembly
  3. Loading assembly instance from disk

1. Getting assembly instance from type information

We can pass reference to an already created object like this:

Program p = new Program();
Assembly assembly = p.GetType().Assembly;

We can also get assembly instance by giving the class name.

Assembly assembly = typeof(Program).Assembly;

2. Getting assembly instance from current assembly and parent assembly

Currently executing assembly instance


For getting the first executable assembly which contains the Main method by which the application starts:


For getting the instance of parent assembly which called current assembly:


3. Loading assembly instance from disk

Assembly a = Assembly.LoadFile(@"C:\ReflectionLibrary.DLL");

Assembly important properties and methods

Assembly class provides various properties to get useful information about the assembly. Below is the list of some useful properties.

Properties Description
Codebase Returns the location of the assembly.
EntryPoint Returns the method information of the entry methods (Main method). Console applications returns reference to the Main method and dynamic link libraries (DLL) returns NULL.
FullName Returns the full name of the assembly including version number, Culture and PublicKeyToken.
GlobalAssemblyCache Bool property. Returns true if assembly loaded from GAC else returns false.
CustomAttributes Returns all the custom attributes of the assembly.
DefinedTypes Returns all the types in the assembly.
Modules Return all the modules in the assembly.

Join our newsletter and get an occasional email with a technology and DotNetPattern.com news update.