System.Reflection.Assembly

Provides information about assemblies and the modules and types they contain.

Class Assembly

Namespace: System.Reflection

Assembly: System.Private.CoreLib

Summary

Represents an assembly, which is a reusable, versionable, and self-describing collection of types and resources that form a logical unit of an application. The Assembly class provides a way to discover information about an assembly, such as its name, version, culture, and public key token.

Assemblies are the fundamental unit of deployment, versioning, reuse, activation, and security policy for the .NET Framework.

Syntax

public abstract class Assembly : object,
    System.Reflection.ICustomAttributeProvider,
    System.Runtime.InteropServices._Assembly

Remarks

The Assembly class is an abstract class. Concrete implementations are provided by the .NET runtime. You typically obtain an Assembly object through static methods of the Assembly class, such as Assembly.Load() or Assembly.GetExecutingAssembly().

Key uses of the Assembly class include:

Members

Properties

Name Description
CodeBase Gets the uniform resource identifier (URI) for the version-dependent or version-independent code base of the assembly, such as the file path or URL.
EntryPoint Gets the entry point of the executable assembly.
FullName Gets the full name of the assembly, which includes the name, version, culture, and public key token.
IsDynamic Gets a value indicating whether the assembly was generated dynamically.
IsFullyTrusted Gets a value indicating whether the assembly has full trust.
Location Gets the full path or UNC name of the code file that contains the manifest.
ManifestModule Gets the module that contains the manifest for this assembly.
SecurityRuleSet Gets the security rule set that applies to the assembly.

Methods

Name Description
CreateInstance(string assemblyName) Creates an instance of the specified type.
Equals(object obj) Determines whether the specified object is equal to the current object.
GetAssembly(Type type) Gets the Assembly in which the specified type is defined.
GetCallingAssembly() Gets the assembly that contains the code that is calling this method.
GetCustomAttributes(bool inherit) Gets all custom attributes for the current assembly.
GetCustomAttributes(Type attributeType, bool inherit) Gets custom attributes of a specified type that are attached to the current assembly.
GetExportedTypes() Gets all the public types defined in the assembly that are available to the world at large.
GetExecutingAssembly() Gets the assembly that is being executed.
GetFile(string name) Gets a specified file from the list of all files in the assembly.
GetFiles() Gets the names of all the files that are associated with this assembly.
GetHashCode() Serves as the default hash function.
GetImageRuntimeValue() (Obsolete) Gets the runtime value of the specified field of the manifest.
GetLoadedModules() Gets all the modules that are loaded in the assembly.
GetModule(string name) Gets the module with the specified name from this assembly.
GetModules() Gets all the modules that make up this assembly.
GetType(string name) Gets the Type with the specified name.
GetType(string name, bool throwOnError) Gets the Type with the specified name, optionally throwing an exception if the type is not found.
GetType(string name, bool throwOnError, bool ignoreCase) Gets the Type with the specified name, optionally throwing an exception and ignoring case.
GetTypes() Gets all the types defined in the current assembly.
IsDefined(Type attributeType, bool inherit) Indicates whether or not to apply a specified custom attribute to the current assembly.
Load(byte[] rawAssembly) Loads an assembly from an array of common intermediate language (CIL) binary text.
Load(string assemblyString) Loads an assembly from the file or URL specified by the assembly name.
LoadFile(string path) Loads an assembly from the specified file path.
LoadFrom(string assemblyFile) Loads an assembly from the specified file, the Assembly object, and the module objects.
ToString() Returns a string that represents the current object.

Fields

The Assembly class has no public fields.

Example

Loading and Inspecting an Assembly

using System;
using System.Reflection;

public class AssemblyExample
{
    public static void Main(string[] args)
    {
        // Get the currently executing assembly
        Assembly currentAssembly = Assembly.GetExecutingAssembly();

        // Get the full name of the assembly
        Console.WriteLine($"Assembly Name: {currentAssembly.FullName}");

        // Get the location of the assembly file
        Console.WriteLine($"Assembly Location: {currentAssembly.Location}");

        // Get all exported types from the assembly
        Type[] types = currentAssembly.GetExportedTypes();
        Console.WriteLine($"\nExported Types ({types.Length}):");
        foreach (Type type in types)
        {
            Console.WriteLine($"- {type.FullName}");
        }

        // Load an assembly from a file path (replace with a valid path to a .dll or .exe)
        try
        {
            string dllPath = @"C:\Path\To\Your\Assembly.dll"; // Example path
            Assembly loadedAssembly = Assembly.LoadFrom(dllPath);
            Console.WriteLine($"\nSuccessfully loaded assembly: {loadedAssembly.FullName}");
        }
        catch (System.IO.FileNotFoundException)
        {
            Console.WriteLine($"\nError: Assembly file not found at the specified path.");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"\nAn error occurred while loading the assembly: {ex.Message}");
        }
    }
}

Requirements

Namespace: System.Reflection

Assembly: System.Private.CoreLib (in System.Private.CoreLib.dll)

See Also