What is a module in .NET?

C#.NetModule

C# Problem Overview


What exactly is a module? What is the difference between a module, a class and a function? How can I access a module in C#?

I am asking this because I want to calculate a checksum of the IL code of only some particular functions, at runtime (without using code signing).

C# Solutions


Solution 1 - C#

A module is a logical collection of code within an Assembly. You can have multiple modules inside an Assembly, and each module can be written in different .NET languages (VS, as far as I'm aware, doesn't support creation of multi-module assemblies).

Assemblies contain modules. Modules contain classes. Classes contain functions.

Yes you can access assemblies, modules, classes, functions, properties, fields etc all via reflection at runtime.

Solution 2 - C#

As an addition to the other answers:

The MSDN states that: "A module is a Microsoft intermediate language (MSIL) file that does not have an assembly manifest.".

Modules can be "linked" together by generating an assembly manifest using the Assembly Linker (al.exe) utility. If i remember it correctly the CLR can load individual modules for an assembly, so that only the neccessary modules get loaded.

EDIT: Found a better description of the Netmodules and why you would want them.

There is another question here on SO that touches the checksum subject. The answers mentions using the GetILAsByteArray method for getting the IL.

Solution 3 - C#

A file

That's what a module is.

module: A single file containing content that can be executed by the VES

(Where VES is a program which reads .NET assembly and converts it to machine code.) see http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf Partition I page 16.

--

An assembly is coherent collection of files in the filesystem (modules). See http://msdn.microsoft.com/en-us/library/zst29sk2(vs.71).aspx

Obviously class definitions are defined inside the file (module) itelf.

Solution 4 - C#

Also there is a VB "module" statement which is not related to assemblies and compilation stuff and is similar to C# static class:

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/module-statement

> A Module statement defines a reference type available throughout its namespace. A module (sometimes called a standard module)is similar to a class but with some important distinctions. Every module has exactly one instance and does not need to be created or assigned to a variable. Modules do not support inheritance or implement interfaces. Notice that a module is not a type in the sense that a class or structure is — you cannot declare a programming element to have the data type of a module. > > You can use Module only at namespace level. This means the declaration > context for a module must be a source file or namespace, and cannot be > a class, structure, module, interface, procedure, or block. You cannot > nest a module within another module, or within any type. For more > information, see Declaration Contexts and Default Access Levels. > > A module has the same lifetime as your program. Because its members > are all Shared, they also have lifetimes equal to that of the program. > > Modules default to Friend access. You can adjust their access levels > with the access modifiers. For more information, see Access levels in > Visual Basic. > > All members of a module are implicitly Shared. >

In short modules in VB are analogs for C# static classes

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPushkarView Question on Stackoverflow
Solution 1 - C#OJ.View Answer on Stackoverflow
Solution 2 - C#PHeibergView Answer on Stackoverflow
Solution 3 - C#Alexander BirdView Answer on Stackoverflow
Solution 4 - C#SENyaView Answer on Stackoverflow