C# .NET 中的模块是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/645728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:35:39  来源:igfitidea点击:

What is a module in .NET?

c#.netmodule

提问by Pushkar

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#?

究竟什么是模块?模块、类和函数之间有什么区别?如何在 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).

我问这个是因为我想在运行时计算仅某些特定函数的 IL 代码的校验和(不使用代码签名)。

采纳答案by OJ.

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

模块是程序集中代码的逻辑集合。你可以在一个程序集中有多个模块,每个模块可以用不同的 .NET 语言编写(VS,据我所知,不支持创建多模块程序集)。

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.

是的,您可以在运行时通过反射访问程序集、模块、类、函数、属性、字段等。

回答by PHeiberg

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

MSDN 声明:“模块是没有程序集清单的 Microsoft 中间语言 (MSIL) 文件。”。

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.

通过使用程序集链接器 (al.exe) 实用程序生成程序集清单,可以将模块“链接”在一起。如果我没记错的话,CLR 可以为程序集加载单个模块,以便只加载必要的模块。

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

编辑:找到了对 Netmodules的更好描述以及为什么需要它们。

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

这里还有另一个问题涉及校验和主题。答案提到使用 GetILAsByteArray 方法获取 IL。

回答by Alexander Bird

A file

一份文件

That's what a module is.

这就是模块。

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

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

VES读取 .NET 程序集并将其转换为机器代码的程序在哪里。)请参阅http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdfPartition I 第 16 页。

--

——

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

程序集是文件系统(模块)中文件的一致集合。请参阅http://msdn.microsoft.com/en-us/library/zst29sk2(vs.71).aspx

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

显然,类定义是在文件(模块)itelf 中定义的。

回答by SENya

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

还有一个与程序集和编译无关的 VB“模块”语句,类似于 C# 静态类:

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

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.

Module 语句定义了一个在其命名空间中可用的引用类型。模块(有时称为标准模块)类似于类,但有一些重要的区别。每个模块都有一个实例,不需要创建或分配给变量。模块不支持继承或实现接口。请注意,模块不是类或结构意义上的类型——您不能声明编程元素具有模块的数据类型。

您只能在命名空间级别使用模块。这意味着模块的声明上下文必须是源文件或命名空间,不能是类、结构、模块、接口、过程或块。您不能在另一个模块或任何类型中嵌套一个模块。有关更多信息,请参阅声明上下文和默认访问级别。

模块与您的程序具有相同的生命周期。因为它的成员都是共享的,所以它们的生命周期也与程序的生命周期相同。

模块默认为好友访问。您可以使用访问修饰符调整他们的访问级别。有关详细信息,请参阅 Visual Basic 中的访问级别。

模块的所有成员都是隐式共享的。

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

简而言之,VB 中的模块类似于 C# 静态类