C# Reference ยท Reflection, Attributes and Metadata
The Assembly class
Reference for System.Reflection.Assembly in C#: get the current or any assembly, list its types, read its name and version, and load assemblies by name.
An assembly is the unit of deployment in .NET: a .dll or .exe that holds compiled code, metadata and resources. System.Reflection.Assembly represents a loaded assembly at run time. Through it you can read the name and version, list every type it defines, find one type by name, and load other assemblies.
You usually get an assembly from a type (typeof(Foo).Assembly), from Assembly.GetExecutingAssembly() (the code that is running right now), or from Assembly.GetEntryAssembly() (the program's entry point). Plug-in systems, test runners and dependency injection scanners use assemblies to discover types.
Assembly asm = typeof(Program).Assembly; foreach (Type t in asm.GetTypes()) { /* ... */ }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public abstract class Assembly { public static Assembly GetExecutingAssembly(); public static Assembly? GetEntryAssembly(); public static Assembly GetCallingAssembly(); public static Assembly Load(string assemblyString); public static Assembly LoadFrom(string assemblyFile); public virtual Type[] GetTypes(); public virtual Type[] GetExportedTypes(); public virtual Type? GetType(string name); public virtual AssemblyName GetName(); }
| Member | Description |
|---|---|
GetExecutingAssembly() |
The assembly that contains the code calling this method. |
GetEntryAssembly() |
The assembly with the program's entry point; can be null when started from unmanaged code. |
GetCallingAssembly() |
The assembly of the caller of the current method. Inlining can change the result, so avoid it for security decisions. |
Load(name) |
Loads by display name ("System.Linq") using the normal probing rules. |
LoadFrom(path) / Assembly.LoadFile(path) |
Loads from a file path. |
GetTypes() / GetExportedTypes() |
All types, or only the public ones. |
GetType(fullName) |
One type by its namespace-qualified name; null if missing. |
GetName() |
An AssemblyName with Name, Version, culture and public key token. |
GetCustomAttributes(), GetManifestResourceNames() |
Attributes on the assembly and embedded resources. |
Returns: an Assembly, arrays of types, or null for single lookups that find nothing.
Errors: Load and LoadFrom throw FileNotFoundException when the assembly cannot be found and BadImageFormatException when the file is not a valid managed assembly. GetTypes() throws ReflectionTypeLoadException if some types cannot be loaded because a dependency is missing; its Types property still holds the types that did load.
Usage notes
- Where your code lives: the runner compiles
main.csinto one assembly, soGetExecutingAssembly()returns it andGetTypes()lists your classes plus compiler-generated ones. - Compiler-generated types (closure classes, state machines, the top-level statements class) appear in
GetTypes(). Their names contain<and>; filter on namespace,IsPublic, or an attribute. - Core types are in
System.Private.CoreLib, an implementation detail. Reference assemblies such asSystem.Runtimeforward to it, so do not hard-code the name in logic. LoadversusLoadFrom: preferLoadwith a name, orAssemblyLoadContextfor isolation and unloading in plug-in scenarios.LoadFromresolves dependencies from the file's folder.- An assembly is loaded once per load context, so loading it again returns the same object.
- Version and location:
GetName().Versionis the assembly version. TheLocationproperty is empty for assemblies loaded from memory or single-file bundles. - Trimming and Native AOT: scanning for types by reflection may miss code that the trimmer removed. Source generators are the safer alternative.
- Security: only load assemblies you trust. Loading runs static initializers and gives the code the same permissions as yours.