Examining the Modules in an Assembly using Reflection in .NET

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT BEST PRACTICES WITH MICROSOFT STACK & ANGULAR

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: