Working with Assemblies in C# – Bestdotnettraining Introduction:
This article explains the Libraries in .NET, Types of DLL, and Assemblies in .NET and its structure, Namespaces and Internal Access Specifier.
Description:
I. Introduction to Libraries
Dynamic Linking Library: A DLL is an application which can have everything that an EXE can have. The difference between them is, an EXE can execute independently but DLL cannot. The code in the DLL can be reused in many other applications but the code in EXE cannot be reused.
II. Types of DLL in Windows OS:
1. Win32 DLL: Here the code is available in the form of simple “C” functions.
2. COM DLL: This DLL has code in the form of reusable COM Components. These are also referred to as ActiveX DLL
3. .NET DLL: These DLL’s (Portable Executables) are used for distributing the reusable classes to various types of .NET applications.
Note:
• The Portable Executable (PE) format is a file format for executables, object code, DLLs, FON Font files and others used in 32-bit and 64-bit versions of Windows operating systems
• PE is a modified version of the Unix COFF file format. PE/COFF is an alternative term in Windows development.
• Microsoft’s .NET Framework has extended the PE format with features which support the Common Language Runtime (CLR).
• The CLR section of it contains two important segments: Metadata and Intermediate Language (IL) code
III. What is Assembly in .NET
An assembly is a collection of types (classes, interface, struct etc…) and resources (bmp, jpg, string table, txt, ico) that are built to work together and form a logical unit of functionality. An assembly provides the common language runtime with the information it needs to be aware of type implementations. To the runtime, a type does not exist outside the context of an assembly.
Assemblies are a fundamental part of programming with the .NET Framework.
• It contains code that the common language runtime executes.
• It forms a security boundary.
• It forms a type of boundary.
• It forms a reference scope boundary.
• It forms a version boundary.
• It forms a deployment unit.
• It is the unit at which side-by-side execution is supported.
Note: We should create a multifile assembly when you want to combine modules written in different languages and to optimize downloading an application by putting seldom-used types in a module that is downloaded only when needed
Note The files that make up a multifile assembly are not physically linked by the file system. Rather, they are linked through the assembly manifest and the common language runtime manages them as a unit.
Assembly Manifest Content:
1. Assembly Name
2. Version Number
3. Culture
4. Strong name information
5. List of all files in an assembly
6. Type reference information of types exported from the assembly
7. Information about other referenced assemblies
IV. About Namespaces
• A namespace is a logical collection of classes and other types with unique names. In a given namespace all the types have a unique name and thus it is used to resolve the ambiguity in case of a name conflict.
• Any type when used outside the namespace, it must be qualified by its namespace, but when used by another type within the same namespace it need not be qualified by the namespace.
• We can use using Namespace on top of the file so that we don’t have to qualify all the types of that namespace in that file explicitly.
V. Internal Access Specifier
• A public Class is accessible within and outside the assembly. Whereas an internal class can be accessed only by other types within the same assembly (irrespective of the namespace of other classes within the assembly).
• Internal is the default access specifier for the class.
• The top-level class can only be declared as either public or internal but nested class (a class within class) can have any access modifier.
• The namespace can never be used as a boundary for an access specifier. It’s used only as a name qualifier.
• An internal member of a class is accessible to all other types within the same assembly and is not accessible outside the assembly.
• A member declared as “protected internal” is accessible to all the class within the assembly and only to the derived classes outside the assembly.
• A public method of a public class cannot have either as a parameter or return type any data type of the same assembly which is not public (or is internal).
VI. Types of Assemblies:
1. Private Assemblies: An assembly whose local copy is maintained by each & every application referencing to it.
2. Shared Assembly: An assembly whose single copy deployed in Global Assembly Cache (GAC) and used/shared by many applications running on that machine. Such assemblies must have a Strong name.
3. Satellite Assembly: A resource-only assembly for a given culture is called as a satellite assembly. They don’t have any code but they have only resources like string tables and images.
4. Dynamic Assemblies: These run directly from memory and are not saved to disk before execution. You can save dynamic assemblies to disk after they have executed. The API method Reflection. Emit is used to create dynamic assemblies