What are Assemblies in .NET?

22 November 2021 at 10:00 by ParTech Media - Post a comment

An assembly is a logical unit that contains.NET Framework-targeted compiled code. You can create an assembly from one or more source code files in .NET and .NET Framework. Also, assemblies part of the .NET Framework constitutes one or more modules. This enables larger projects to be organized in such a way that multiple developers can work on different source code files or modules, which are then integrated to form a single assembly. So, let us quickly begin with this article on assemblies in the .NET framework.

Table of contents

  1. What is a .Net Assembly?
  2. Private Assembly
  3. Shared Assembly
  4. Features of Assembly
  5. Basic Structure of an Assembly
  6. Conclusion

What is a .Net Assembly?

The .NET assembly is the industry standard for Microsoft.NET components. .NET assemblies can be either:

  • Executable (.exe) files
  • Dynamic Link Library (DLL) files

The definition of types, versioning information for the type, meta-data, and manifest are all contained in the.NET assemblies. The component (assembly) resolution has received a lot of attention from the.NET designers. An assembly can either be a single file or a collection of files.

When working with multi-file assemblies, there is one master module that contains the manifest, and other assemblies exist as non-manifest modules. In .NET, a module is a component of a multi-file assembly. Along with reflections and attributes, assembly is one of the most fascinating and valuable aspects of the .NET architecture.

Here is a simple hello world code:-

Now to host the signed assembly in Global Assembly Cache, run the following commands in the command prompt:

Types of Assembly

Private Assembly

Private assemblies demand us to copy them separately in all application directories where we wish to use their features; if we don't copy them, we won't be able to use their features and power. Every time we have a private assembly, we only copy it into the BIN folder of each application folder.

Shared Assembly

It is not necessary to duplicate each application folder separately for public assembly. Shared Assembly is another name for public assembly. There is only one copy necessary at the system level; the assembly does not need to be copied into the application folder. In GAC, a public assembly should be installed.

Strong named assemblies (aka shared assemblies) are typically copied to a single location (usually the Global assembly cache). The identical copy of the shared assembly is used from its original location for all calling assemblies within the same application. This simply means - shared assemblies are not transferred into the private folder of each calling assembly. Each shared assembly comprises of -

  • Face name
  • Version
  • Public key token
  • Culture data

Because of the public key token and version information, mixing two separate assemblies with the same name or two comparable assemblies with different versions is nearly impossible.

GAC (Global Assembly Cache):- When an assembly is required for multiple projects or applications, we must create it with a strong name and store it in GAC or the Assembly folder by using the GACUtil command to install it.

Features of Assembly

The following are the typical characteristics of an assembly:

  • Assemblies are self-explanatory. All of the information about the assembly is kept in the assembly itself.
  • The assembly manifest keeps track of version dependencies.
  • Side-by-side loading of assemblies is possible.
  • Installing an assembly can be as simple as copying the files that make up the assembly.
  • Assemblies might be private or open to the public.

Basic Structure of an Assembly

The following are the four sections that make up an assembly:

  • Assembly Metadata
  • Type Metadata
  • Microsoft Intermediate Language (MSIL)
  • Resources

Metadata for the Assembly: The information for the assembly as a whole explains the entire thing. The assembly manifest is another name for the assembly metadata. The following are some of the contents of the assembly metadata:

  • The assembly's official name.
  • The number of the version.
  • The assembly's preferred culture.
  • The public key and digital signature are two types of digital signatures. These offer a unique identifier for the assembly's creator.
  • All the files that make up the assembly are listed here.
  • All referenced assemblies are listed here.
  • All exported classes, methods, properties, and other items in the assembly have reference information.

Type Metadata: The types in the assembly are described by type metadata. It also includes all types and resources that are available to the public. Files such as BMP or JPG files, as well as any other file required by the application, are referred to as resources.

Microsoft intermediate language (MSIL): MSIL is the key to .NET capability to be language neutral. No matter what programming language is used, every code is compiled into the same MSIL. Because all languages eventually compile to the same MSIL, encapsulation, inheritance, polymorphism, exception handling, debugging, and other features can now be language agnostic. MSIL is also the fundamental reason for .NET's ability to run independently. MSIL provides the possibility to "write once, run anywhere."

Resources: Resources in .NET can be placed in one of two locations: an external resources file or natively within an assembly. Accessing the resources in either location is extremely simple. Here are a few important classes in it -

  • ResourceManager: Within an assembly, ResourceManager is used to access resources.
  • ResourceWriter: A resource writer is a program that writes resources to an external.resource file.
  • ResourceReader: A resource reader is a program that reads materials from the internet.

Aside from these classes, the.NET framework includes the resgen.exe program, which produces a resource file from a text file containing key/value pairs.

Final Words

Remember - You must reference an assembly in order to use it in an application. All the accessible types, properties, methods, and other members of an assembly's namespaces are available to your application as if their code were part of your source file once it is referenced.

Latest