16.4. Retrieving modules from an assembly

An assembly can consist of one or more modules.[1] To obtain an array of modules in an assembly, you use System.Reflection.Assembly's GetModules method.

[1] A module is a file containing IL codes but without a manifest. Each assembly must have one and only one manifest file regardless of the number of modules it contains.

Let's create an assembly (called Assm) containing two modules (Mod1 and Mod2) first. The following three code fragments do just that.

1: // Mod1.cs
2: public class Mod1{}
1: // Mod2.cs
2: public class Mod2{}
 1: // Assm.cs
 2: using System;
 3: using System.Reflection;
 4:
 5: public class Assm{
 6:   public static void Main(){
 7:     Assembly a = Assembly.LoadFrom("Assm.exe");
 8:     Module[] modules ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.