C#
.NET
Reflection
Assemblies
Programming

How do you loop through currently loaded assemblies?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In software development, particularly with .NET applications, you may find yourself needing to examine or work with assemblies dynamically. An assembly is a compiled code library used for deployment, versioning, and security in .NET applications. It’s common to loop through currently loaded assemblies when you're performing tasks such as dependency injection, dynamic type discovery, or plugins implementation. This article explores how you can loop through currently loaded assemblies using C#.

Understanding Assemblies in .NET

Before delving into code examples, it’s important to understand what an assembly is. An assembly contains one or more managed code modules and a manifest, which provides metadata about the modules. It includes details like the version number, culture, and the strong name.

Assemblies can be static (.exe or .dll files) or dynamic (loaded at runtime). Reflecting over assemblies allows you to inquire about the types they contain, the members of these types, and other characteristics.

Looping through Loaded Assemblies

Technical Explanation

Assemblies in a .NET application are loaded into the AppDomain. To loop through these assemblies, you need to access the `AppDomain`'s `GetAssemblies` method, which returns an array of all loaded assemblies in the current application domain.

Here's a basic step-by-step process:

  1. Get the Current AppDomain: Access using `AppDomain.CurrentDomain`.
  2. Retrieve Assemblies: Use `GetAssemblies` method to retrieve all currently loaded assemblies within the application domain.
  3. Loop Through Assemblies: Iterate through the array using a `foreach` loop to access individual `Assembly` objects.
  4. Access Assembly Information: For each `Assembly`, use reflection to examine contained types or metadata.

Example Code

Here is an example in C# that demonstrates how to loop through currently loaded assemblies and print their full names:

  • Attribute Discovery: Use reflection to find and utilize custom attributes.
  • Dynamic Type Instantiation: Learn how to create instances of types at runtime.
  • Lazy Loading: Strategies to load assemblies only when they are needed.
  • Memory Usage: Considerations for working with a large number of assemblies.
  • Strong-Naming: Understand how assemblies can be signed for authenticity.
  • GAC: Explore how assemblies are managed globally in the Global Assembly Cache.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.