Wednesday, May 4, 2011

Parts of a .NET Assembly (Anatomy of a .NET Assembly)


A .NET Assembly (DLL or EXE) consists of the following 4 Parts
  1. Assembly metadata (Also known as the "Manifest")
  2. Type metadata 
  3. IL code (the partially compiled code that runs off the CLR)
  4. Resources 
The Assembly metadata (or Manifest) is nothing but a 4-part name of the assembly that uniquely identifies a .NET Assembly (be it shared or private)


A .NET Assembly reference is said to be fully qualified if all the following 4 parts of the Manifest are specified.

  1.  Assembly Name ( a friendly name)
  2. Version
  3. Culture Information
  4. Public Key Token (in case of a shared assembly). This is actually the developer information.

Steps for creating a Shared (or Public) Assembly

"In order to turn a private assembly into a shared-assembly, it has to be installed on the GAC and only those assemblies can be added to the GAC that have strong names."

  1. Create a fresh Key Pair File (Public-Private keys) using the Strong Name utility (sn.exe)
  2. Sign the assembly (to be shared on GAC) with this file and compile the assembly. (once compiled, the assembly will have a "Strong Name". A strongly-named assembly will have a Public Key associated with it that uniquely identifies it on that computer. Since the Public key is 128 byte long ( plus 32 bytes of header information), its 8-byte long hash is used instead to identify an assembly. This 8-byte long hash is called the Public Key Token. 
  3. Only a strongly-named assembly is allowed to be added to the Global Assembly Cashe (GAC). So once we have a strongly-named assembly, we can then use a utility such as gacutil to install the assembly into the GAC.
  4. Once the assembly is installed in the GAC, it becomes a shared-assembly. This effectively means that we can write other applications that can reference and make use of this shared-assembly without having to maintain a separate local copy in their respective application folders.

Read the following articles for additional information.
Link1
Link2
Link3

No comments:

Post a Comment