views:

675

answers:

4

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

  1. Where does it begin and where does it end?
  2. What important information should I know about Assemblies?
+7  A: 

.NET assembly

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

Developer Art
+33  A: 

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.

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.

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.

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.

marc_s
@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.
Greg D
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
Pratik
@marc_s: I would appreciate it if you add @Pratik's point to the answer, thanks
Roee Adler
@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....)
marc_s
@GregD: it seems to be "multi-file assembly" after all - check out http://msdn.microsoft.com/en-us/library/226t7yxe.aspx
marc_s
"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).
Ash
+3  A: 

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.

Raghav Khunger
Assemblies are distinct from the files that contain them.
Greg D
@Raghav - Update your photograph with something casual for heavens sake! We're not posing here for passports, you know!
Jenko
+1  A: 

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

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 is as follows:

PE header
CLR header
CLR metadata
CLR
IL code
Native data

Chris S