gpt4 book ai didi

What exactly is an Assembly in C# or .NET?(C#或.NET中的程序集到底是什么?)

转载 作者:bug小助手 更新时间:2023-10-25 14:39:57 24 4
gpt4 key购买 nike



Could you please explain what is an Assembly in C# or .NET?

您能解释一下什么是C#或.NET中的程序集吗?




  1. Where does it begin and where does it end?

  2. What important information should I know about Assemblies?


更多回答

Strictly speaking, isn't an assembly a .NET concept rather than a C# concept?

严格地说,程序集不是.NET概念而不是C#概念吗?

@JeffH: when I asked the question I thought it was a C# concept. Now I understand it's a .NET one. Nevertheless, since I think other people may be confused like I was, I intentionally did not change it for Google to accommodate for this mistake...

@JeffH:当我问这个问题时,我以为这是一个C#概念。现在我明白了,这是一个.NET版本。然而,由于我认为其他人可能会像我一样感到困惑,我故意没有为谷歌改变它,以适应这个错误……

It's a C# concept as well. You have to understand it to use "internal" access modifier properly.

它也是一个C#概念。您必须理解它才能正确使用“内部”访问修饰符。

My grain of salt: stackoverflow.com/a/54803483/7389293

我的想法:Stackoverflow.com/a/54803483/7389293

优秀答案推荐

An assembly is the compiled output of your code, typically a DLL, but your EXE is also an assembly. It's the smallest unit of deployment for any .NET project.

程序集是代码的编译输出,通常是DLL,但您的EXE也是程序集。它是任何.NET项目中最小的部署单元。



The assembly typically contains .NET code in MSIL (Microsoft Intermediate language) that will be compiled to native code ("JITted" - compiled by the Just-In-Time compiler) the first time it is executed on a given machine. That compiled code will also be stored in the assembly and reused on subsequent calls.

程序集通常包含MSIL(Microsoft中间语言)格式的.NET代码,在给定计算机上第一次执行时,这些代码将被编译为本机代码(“JITted”-由实时编译器编译)。编译后的代码也将存储在程序集中,并在后续调用中重复使用。



The assembly can also contain resources like icons, bitmaps, string tables and so on. Furthermore, the assembly also contains metadata in the assembly manifest - information like version number, strong name, culture, referenced assemblies and so forth.

程序集还可以包含图标、位图、字符串表等资源。此外,程序集还包含程序集清单中的元数据,如版本号、强名称、区域性、引用的程序集等信息。



In 99% of your cases, one assembly equals a physical file on disk - the case of a multi-file assembly (one assembly, distributed across more than a single file) appears to be a rather odd-ball edge case which I've never encountered so far in my 5+ years of .NET development.

在99%的情况下,一个程序集相当于磁盘上的一个物理文件--多文件程序集(一个程序集,分布在多个文件中)似乎是一个相当奇怪的边缘情况,在我5年多的.NET开发生涯中,到目前为止还没有遇到过这种情况。



In a multifile assembly there would still be only one assembly manifest in a DLL or EXE and the MSIL code in multiple netmodule files.

在多文件程序集中,DLL或EXE中仍将只有一个程序集清单,而多个NetModule文件中仍将有MSIL代码。



The answer is in order for immediate-grasping.

答案是,必须立即抓住。




Put simply, it is the compiled project involving your classes and additional files, if there are. That is, each project in a
solution is assembly.




Or more techinally,

或者更专业地说,




An assembly is where a type is stored in the flesystem. Assemblies are
a mechanism for deploying code. For example, the System.Data.dll
assembly contains types for managing data. To use types in other
assemblies, they must be referenced. - Source




How do we know it? If you glance at properties of a project under the solution you can see the following images.

我们怎么知道的?如果您浏览解决方案下项目的属性,您可以看到以下图像。



When you compile the project, it turns out to DLL or EXE.

当您编译项目时,结果是DLL或EXE。



enter image description here
enter image description here
enter image description here



.NET assembly

.NET程序集




In the Microsoft .NET framework, an
assembly is a partially compiled code
library for use in deployment,
versioning and security.




http://www.codeguru.com/columns/csharp_learning/article.php/c5845

Http://www.codeguru.com/columns/csharp_learning/article.php/c5845



An assembly is a file that is automatically generated by the compiler upon successful compilation of every .NET application. It can be either a Dynamic Link Library or an executable file. It is generated only once for an application and upon each subsequent compilation the assembly gets updated.

程序集是编译器在成功编译每个.NET应用程序时自动生成的文件。它可以是动态链接库或可执行文件。它只为应用程序生成一次,并且在每次后续编译时都会更新程序集。



Here's another explanation of the make up of .NET Assemblies, a mini-quote:

以下是对.NET程序集组成的另一种解释,这是一句小引语:




The .NET framework consists of the
concepts of modules, assemblies, which
both store metadata and manifest
information. An assembly can contain
multiple modules. Visual C# only ever
creates one module which is turned
into an assembly by the C# compiler
(csc.exe), but an assembly can link
many .NET modules together via the
assembly linker (al.exe) command-line
tool. For example each of your source
code .cs files could be compiled into
a module and linked together to form
an assembly - an assembly is just a
collection of modules and resources.
One of these modules, however; must
contain manifest metadata (see below)
information for the assembly to be
understood by the CLR.

....

Having created a new .exe or .dll
inside VS.NET you see your file appear
inside your bin folder. Opening it in
notepad will give out gibberish, or
even inside a hexadecimal editor
without knowing the structure of the
file, you need a tool like ildasm.exe
or CFF explorer to make meaning from
it. The structure of the assembly is
as follows:



PE header

CLR header

CLR metadata

CLR

IL code

Native data




When a source code is compiled by the language compiler it Generate a Managed Assembly and MSIL(MisroSoft Intermediate Language). That Assembly contains .dll or .exe file. An Assebmly can be of two types Private Assembly and Shared Assembly, shared Assembly is stored in GAC(Global Assembly Cache) so that any application can refer to it while private assembly is stored in application folder which can be used by only one Application.

当源代码由语言编译器编译时,它会生成托管程序集和MSIL(MisroSoft中间语言)。该程序集包含.dll或.exe文件。Assebmly可以分为私有程序集和共享程序集两种类型,共享程序集存储在GAC(全局程序集缓存)中,以便任何应用程序都可以引用它,而私有程序集存储在只能由一个应用程序使用的应用程序文件夹中。



An assembly is a DLL or an EXE which will be created when you publish it or compile your application.

程序集是在发布或编译应用程序时创建的DLL或EXE。



After writing source code of your program(project) then a file is created which may be DLL or EXE depends on your project. It makes only once for a single project. It has two types
1:- single
2:- shared or multiprogram
single assembly used only in a single program while shared can be used for multiprogram

在编写了程序(项目)的源代码后,就会创建一个文件,该文件可以是DLL或EXE,具体取决于您的项目。它只为一个项目制造一次。它有两种类型1:-单一2:-共享或多程序单一汇编仅用于单程序,而共享可用于多程序



An Assembly is a collection of logical units. Logical units refer to the types and resources which are required to build an application and deploy them using the .Net framework. Basically, Assembly is a collection of Exe and DLLs. It is portable and executable.

程序集是逻辑单元的集合。逻辑单元指的是构建应用程序并使用.Net框架部署它们所需的类型和资源。基本上,ASSEMBLY是EXE和DLL的集合。它是可移植和可执行的。



Assemblies are a collection of different files created by CSC (C# compiler) whenever you compile a '.cs' file. If the .cs file contained a main method, .exe (executable) would be part of that assembly, else .dll (dynamically linked library) is part of that assembly. Apart from .exe/.dll, assemblies contain the manifest file (metadata of the assembly), resource namespaces (contain language-specific code alterations, for eg. resource files for French, Arabic, etc.), satellite files (separate assembly files for a culture/language). These assembly code files are written in MSIL (Microsoft Intermediate Language), an intermediate code similar to a Java bytecode.

程序集是CSC(C#编译器)在编译‘.cs’文件时创建的不同文件的集合。如果.cs文件包含Main方法,则.exe(可执行文件)将是该程序集的一部分,否则.dll(动态链接库)将是该程序集的一部分。除了.exe/.dll之外,程序集还包含清单文件(程序集的元数据)、资源命名空间(包含特定于语言的代码更改,例如。法语、阿拉伯语等的资源文件)、附属文件(文化/语言的单独汇编文件)。这些汇编代码文件是用MSIL(微软中间语言)编写的,MSIL是一种类似于Java字节码的中间代码。


更多回答

@marc_s: I think you mean a multi-assembly file. It's possible to package multiple assemblies within a single file via studio's command-line tools, but not directly via the IDE.

@MARC_S:我想你是指多部件文件。可以通过Studio的命令行工具将多个程序集打包到一个文件中,但不能直接通过IDE。

A very important thing in an assembly is the metadata in the assembly manifest. The manifest has information like version, strong name, culture, referenced assemblies etc. In a multifile assembly there would still be only one assembly manifest in a dll or exe and the MSIL code in multiple .netmodule files. Basically an assembly is minimum unit of deployment in >net

程序集中非常重要的一件事是程序集清单中的元数据。清单包含版本、强名称、区域性、引用的程序集等信息。在多文件程序集中,DLL或EXE中仍然只有一个程序集清单,而多个.net模块文件中仍有MSIL代码。基本上,程序集是>Net中的最小部署单元

@marc_s: I would appreciate it if you add @Pratik's point to the answer, thanks

@MARC_S:如果您在答案中加上@普拉蒂克的观点,我将不胜感激,谢谢

@rax: done; @pratik: now you're confusing me: you talk about "multi-file assembly" again, while @GregD mentioned it really was a "multi-assembly file" - what is it now?? (as I said - I've never encountered such a beast, so I'm a bit unclear whether it's multiple assemblies per file, or multiple files per assembly....)

@rax:Done;@Patik:现在你又把我搞糊涂了:你又说到“多文件汇编”,而@GregD提到它真的是一个“多汇编文件”--现在是什么??(正如我所说-我从未遇到过这样的野兽,所以我有点不清楚是每个文件多个程序集,还是每个程序集多个文件……)

"That compiled code will also be stored in the assembly and reused on subsequent calls." This is misleading and/or not very clear. No compiled code is "stored" in an assembly. I think you may be referring to the way each method/function is "jitted" to native code when it is first called. Then, while the assembly is in memory, the native code version of the method is called. If the assembly is unloaded, and later reloaded, the jit process will occur all over again. Of course this assumes that you are not using NGEN to pre-compile your assemblies to native code (not recommended).

“编译后的代码也将存储在程序集中,并在后续调用中重复使用。”这具有误导性和/或不是很清楚。任何已编译的代码都不会“存储”在程序集中。我想您可能指的是每个方法/函数在第一次被调用时被“jit”到本机代码的方式。然后,当程序集在内存中时,将调用该方法的本机代码版本。如果卸载程序集,然后重新加载,则JIT过程将重新进行。当然,这假设您没有使用ngen将程序集预编译为本机代码(不推荐)。

Is there a way to locate the actual assembly file from within the IDE? I know it can be located from the Windows Explorer, in the bin folder inside the project, but this is an unpractical way of finding the files.

有没有办法从IDE中找到实际的程序集文件?我知道它可以从Windows资源管理器中的项目内的bin文件夹中找到,但这是一种不切实际的查找文件的方式。

@carloswm85 why don't you simply right click and lovate its folder? i.stack.imgur.com/RzsRB.png

@carloswm85为什么不直接右击并锁定它的文件夹呢?I.stack.imgur.com/RzsRB.png

not the solution I'm looking for. I don't want to have to access to the Windows Explorer. I found a solution, though. From inside VS19: In Solution Explorer click Show All Files menu button, then the bin folder (and others) will appear. Open debug folder, right click on the desired assembly and click Copy Path.

这不是我想要的解决方案。我不想访问Windows资源管理器。不过,我找到了一个解决方案。从VS19内部:在解决方案资源管理器中,单击显示所有文件菜单按钮,然后将出现bin文件夹(和其他文件夹)。打开调试文件夹,右击所需的程序集,然后单击复制路径。

Assemblies are distinct from the files that contain them.

程序集不同于包含它们的文件。

@Raghav - Update your photograph with something casual for heavens sake! We're not posing here for passports, you know!

@Raghav-看在上帝的份上,用一些休闲的东西更新你的照片!我们不是为了护照而摆姿势,你知道的!

Where the hell is that link taking me to? Kind of infinite redirects! After a few redirects it took me to a page that says this. No way to get original the URL you posted either.

那个该死的链接要把我带到哪里去?一种无限重定向!经过几次重定向后,我进入了一个页面,上面写着这一点。也没有办法获得您发布的原始URL。

After writing source code of your program(project) then a file is created which may be DLL or EXE depends on your project - after writing the source code? Doesn't seem to be a very good indication as to when that file is created, which BTW is immaterial given the question.

写完程序(项目)的源代码后,就会创建一个文件,它可以是DLL或EXE,这取决于您的项目--在写完源代码之后?对于何时创建该文件似乎不是一个很好的指示,考虑到这个问题,哪个BTW是无关紧要的。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com