What is a module in .NET?
Asked Answered
K

4

58

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).

Kilovoltampere answered 14/3, 2009 at 10:20 Comment(0)
M
62

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.

Metaxylem answered 14/3, 2009 at 10:29 Comment(2)
Normally (and you have no option in VS) an assembly consists of a single module. Using the command line tools you can, however, create an assembly of multiple modules (one of which will contain the whole assembly's metadata). Can be useful to allow incremental download for clickonce.Valenti
But is it possible a module span across multiple assembly?Heffernan
S
11

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.

Sather answered 14/3, 2009 at 16:59 Comment(0)
C
7

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.

Carlycarlye answered 15/7, 2011 at 14:6 Comment(0)
I
3

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

https://learn.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

Indican answered 21/2, 2018 at 15:18 Comment(3)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted.Flatware
The main topic was obviously devoted to "modules" as a kind of assembly so didn't see the need for detailed answer when it is well described in documentationIndican
Quoting like you did is perfectly valid. It is just to make sure that if the link goes down for whatever reason your answer is not invalid instantly. Thanks for taking the effort!Flatware

© 2022 - 2024 — McMap. All rights reserved.