Examining the Modules in an Assembly using Reflection in .NET
September 19, 2014 Leave a comment
A Module is a container for type information. If you’d like to inspect the Modules available in an assembly, you’ll first need to get a reference to the assembly in question. Once you have the reference to the assembly you can get hold of the modules as follows:
Assembly callingAssembly = Assembly.GetCallingAssembly(); Module[] modulesInCallingAssembly = callingAssembly.GetModules();
You can then iterate through the module array and read its properties:
foreach (Module module in modulesInCallingAssembly) { Console.WriteLine(module.FullyQualifiedName); Console.WriteLine(module.Name); }
In my case, as this is a very simple Console app I got the following output:
C:\Studies\Reflection\ReflectionCodeBits\ReflectionCodeBits\bin\Debug\ReflectionCodeBits.exe
ReflectionCodeBits.exe
Therefore the FullyQualifiedName property returns the full file path to the module. The Name property only returns the name without the file path.
The Module class has a couple of exciting methods to extract the fields and methods attached to it:
MethodInfo[] methods = module.GetMethods(); FieldInfo[] fields = module.GetFields();
We’ll take up FieldInfo and MethodInfo in later blog posts on Reflection to see what we can do with them.
View all posts on Reflection here.