You are on page 1of 57


y y y y y y y y y y

Discuss Assemblies Create Assemblies Private and Shared Assembly Discuss Versioning & Reflection Explain the structure of an assembly Explain Modules Work with Resource files Discuss the global assembly cache Create & Install shared assemblies Discuss versioning

 An assembly is a means of reusing the code.  An assembly consists of:
Set of Types and Resources Manifest

 The manifest of an assembly contains:
Identity of an assembly Names of all the files in the assembly Details of all the types and resources defined in the assembly A hash of all the files in the assembly Names and hashes of other assemblies that this assembly is dependent on

Creating Assemblies
 The following command creates an assembly from one or more source files:

 The following command creates an assembly called array.dll from the array1.cs file:

Creating Executables
 The following command creates an executable by compiling the source file using the /target:exe switch:

 The following command creates an executable array.exe from the array1.cs file:

Referencing an Assembly
 To reference an assembly, add the /reference switch while creating the executable:

 The following command creates an executable called array.exe from the source file array1.cs while referencing an assembly called array2.dll:

 Provides a logical distribution of code
 Avoids naming conflicts.  Elements are designed to help us organize our code.  Reduces complexity when the code is to be reused for some other application.

Namespace vs. Assembly

 Difference between Namespace & Assembly
Logical compile time mechanism Not a runtime entity Physical compile time mechanism A runtime entity

Provides a physical Provides a logical structure to the names of structure to the runtime components of an source code elements executable

Types of Assemblies (1)

Private Assemblies
By default, a C# program is compiled to a private assembly Needs to be placed in the same folder as the calling application Assembly name should be unique within the application

Types of Assemblies (2)

Shared Assemblies
Shared across various applications Assembly name should be unique across all the applications using the assembly Placed in the Global Assembly Cache

Global Assembly Folder

Assemblies and the internal access modifiers

 The internal access modifier can be used to specify accessibility at the assembly level
Internal methods accessible within the same assembly internal class InternalMatter { internal void Method1() { //Method1 Implementation } } class KnowInternalMatter { public GetIM() { InternalMatter.Method1(); } }
Namespace Assem1

Internal methods not accessible from outside the assembly

classExternalGuy { public GetIM() {Assem1.InternalMatter.Method1(); } }

Namespace Assem2

Assembly 2

Assembly 1

 All assemblies must have a version number. The following sequence is also available in some books Major Minor Build Revision

Incompatible with previous versions

Might be compatible with previous versions

Minor change in the assembly

Solving the DLL Hell Problem

 In previous versions of the Microsoft Windows Operating Systems, two DLL files with the same name could not be used in a single process.  The DLL file needed to be upgraded by overwriting the existing DLL file.  Now, Windows 2000 operating system is capable of loading two assemblies with the same name but different version numbers.

Reflections (1)
 Retrieves information about an object at runtime.  The Type class is the base of all reflection information for an object.  Two methods to get the Type object:
typeof() GetType()

Reflections (2)


Reflections (3)


Reflections (4)
 Table showing methods of the Type object

Assembly Structure
y Every assembly has a predefined structure
.NET metadata can be thought of as a collection of information existing in binary format Metadata is a superset of older technologies such asMetadata type libraries and IDL files Type metadata describes the data present in the assemblyType type and its This metadata contains a declaration for every data type and members. It is a specification of the types declared and methods implemented in the assembly .NET compilers compile source code into an intermediate language called MSIL (Microsoft Intermediate Language). It is this MSIL that the MSIL CLR understands The MSIL is stored as a part of the Code assembly Resources are nothing but files required while developing the Resources application For example, the JPG file for the logo of your company. These resources can exist as a part of the .NET assembly or as a separate file



.NET Modules (1)

y The files that contain IL code are known as .NET

modules y Contain Type metadata and the IL code y Assembly metadata is not stored in a module y Can be created using the command line C Sharp compiler csc.exe

.NET Modules (2)

y The syntax to create a .NET module

y This command will generate a module file called

MySource.netmodule y Once a module is generated it can be made a part of any other assembly

.NET Modules (3)

y The syntax for making the module a part of an


y The .NET SDK provides us with yet another tool

called the IL Disassembler

IL Disassembler
y This tool can be used to view the content of assemblies

and manifests

y The assembly A2.Dll will hold the manifest

information and a link to the module file MySource.netmodule

IL Disassembler (Cont .)

Working with resource files

y A resource file is a collection of resources like

images and audio files

y Resources are stored in key-value pairs y The Key identifies the resource by name, and the

value refers to the resource, as in a hash table

Resgen Utility
y The Resgen.exe utility comes along with the .NET framework SDK y This utility is used to create resource files y This command will create a file called


y The resgen utility can also create XML-based resource file (.resX

file) with the following command

ResourceWriter Class
y Using the ResourceWriter class of the .NET

BCL, we can add picture files to an assembly

y Using this class, you can add up to 2 GB of

y The AddResource() method is used to add


using System; using System.Resources; using System.Drawing; namespace AdvanceDotNet { class AacEx1 { [STAThread] static void Main(string[] args) { ResourceWriter ResW = new ResourceWriter("addr.resources"); using(Image logo = Image.FromFile("Bahria.jpg")) { ResW.AddResource("BahriaLogo", logo); ResW.AddResource("Description", "Bahria Corporate Logo"); ResW.Close(); } } }}

Cont .
y Using defining the scope at the end of which an

ResourceWriter object will be disposed

Adding Resources (1)

y To add more resources -

Adding Resources (2)

y This dialog box will pop-up on selecting

Add Existing Item

Using Resources (1)

y Consider this Windows Application y The resource file has to be created for this project

Picture Box


Fetches the images

Using Resources (2)

y Code used in the click event of button
Private void button1_click(object sender, System.EventArgs e) { Assembly as1 = Assembly.GetExecuteingAssembly(); ResourceManager rm = new ResourceManager(AacEx2.addr,as1); pictureBox1.Image=(Image) rm.GetObject(BahriaLogo); label2.Text = rm.GetString(Description); }

y as1 is of assembly type and holds the reference to the

executing assembly y Creating the object of ResourceManager class by passing two parameters the first is the root name of the resource and second is assembly object that hold the reference

Using Resources (3)

y Output

GAC Global Assembly Cache

y The GAC is a cache for Assemblies y It is nothing but a folder y Shared assemblies need to be stored in this folder y Proves useful when it comes to interoperability with

COM and vice versa

Viewing GAC
y The files in the GAC can be viewed using

Windows Explorer y The files are stored in -

Assembly Folder Contents

y The contents of the folder look something like -

The GAC utility

y The GAC utility (gacutil.exe) is used to

manipulate, delete and view assemblies y Can be used to install, uninstall and list assemblies on the GAC using the command line

GAC utility - Options

y The gacutil.exe has 4 options Command Line Option /l /i /u /upre Description Lists the assemblies from the GAC Install the specified assembly on the GAC Uninstall the specified assembly on the GAC Uninstalls the assembly from the native image cache

Shared Assemblies
y An assembly needs to be shared for it to be used by

various applications y There should be no naming clashes with other assemblies y To enable this, the assembly should have a unique identifier y The Strong Name (sn.exe) utility is provided

Strong Name Utility

y This utility generates a strong name or a unique name

using various cryptographic/ encryption algorithms

Creating Shared Assemblies (1)

y Example -

Creating Shared Assemblies (2)

y Next we need to sign this assembly with the strong

name we have created y For this we go to the Solution Explorer and project properties y From left side select signing tap


Creating Shared Assemblies (3)

y Now check sign this assembly and select strong

name key that you have created or create new one.

Installing an Assembly
y To install this signed assembly on the GAC we will use


Using Shared Assemblies (1)

y Example -

Using Shared Assemblies (2)

y The properties can be viewed by right clicking on the

assembly reference in the solution explorer

y Versioning comes into picture when there is an

upgrade of an application and its components

Versioning a .NET assembly

y Version number consists of Major Minor Revision Build

Version Compatibility
Change In Major Number or Minor Number Compatibility

Revision Number

Build Number

Example for versioning

y The AssemblyInfo.cs file contains the version of

the assembly

y Compiling the above assembly will result in the

version of the assembly being changed

Viewing the version (1)

y We can view the assembly version number being

written into the assembly manifest using the ILDASM tool

Viewing the version (2)

y In the GAC, there are two assemblies with the same

name but a different version number

Fetching the version number (1)

y Example -

Fetching the version number (2)

y Output -