Вы находитесь на странице: 1из 57

Objectives

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

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

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:

Namespaces
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 Physical compile time

mechanism
Not a runtime entity Provides a logical

mechanism
A runtime entity

Provides a physical

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

Versioning
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)

continued

Reflections (3)
continued

Output:

Reflections (4)
Table showing methods of the Type object

Assembly Structure
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 as type libraries Metadata 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

Assembly

Metadata

.NET Modules (1)


The files that contain IL code are known as .NET modules Contain Type metadata and the IL code Assembly metadata is not stored in a module Can be created using the command line C Sharp compiler csc.exe

.NET Modules (2)


The syntax to create a .NET module

This command will generate a module file called MySource.netmodule Once a module is generated it can be made a part of any other assembly

.NET Modules (3)


The syntax for making the module a part of an assembly

The .NET SDK provides us with yet another tool called the IL Disassembler

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

and manifests

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


A resource file is a collection of resources like images and audio files Resources are stored in key-value pairs

The Key identifies the resource by name, and the value refers to the resource, as in a hash table

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

addr.resources

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

file) with the following command

ResourceWriter Class
Using the ResourceWriter class of the .NET BCL, we can add picture files to an assembly Using this class, you can add up to 2 GB of resources The AddResource() method is used to add resources.

Example
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.
Using defining the scope at the end of which an

ResourceWriter object will be disposed

Adding Resources (1)


To add more resources -

Adding Resources (2)


This dialog box will pop-up on selecting

Add Existing Item

Using Resources (1)


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

Picture Box

Label

Fetches the images

Using Resources (2)


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); }

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

executing assembly 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)


Output

GAC Global Assembly Cache


The GAC is a cache for Assemblies
It is nothing but a folder Shared assemblies need to be stored in this folder

Proves useful when it comes to interoperability with

COM and vice versa

Viewing GAC
The files in the GAC can be viewed using

Windows Explorer The files are stored in -

Assembly Folder Contents


The contents of the folder look something like -

The GAC utility


The GAC utility (gacutil.exe) is used to

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

GAC utility - Options


The gacutil.exe has 4 options Command Line Option /l /i /u 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

/upre

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

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

Strong Name Utility


This utility generates a strong name or a unique name

using various cryptographic/ encryption algorithms

Creating Shared Assemblies (1)


Example -

Creating Shared Assemblies (2)


Next we need to sign this assembly with the strong name we have created For this we go to the Solution Explorer and project properties

take

From left side select signing tap

Creating Shared Assemblies (3)


Now check sign this assembly and select strong

name key that you have created or create new one.

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

gacutil.exe

Using Shared Assemblies (1)


Example -

Using Shared Assemblies (2)


The properties can be viewed by right clicking on the

assembly reference in the solution explorer

Versioning
Versioning comes into picture when there is an

upgrade of an application and its components

Versioning a .NET assembly


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


The AssemblyInfo.cs file contains the version of the assembly

Compiling the above assembly will result in the version of the assembly being changed

Viewing the version (1)


We can view the assembly version number being

written into the assembly manifest using the ILDASM tool

Viewing the version (2)


In the GAC, there are two assemblies with the same

name but a different version number

Fetching the version number (1)


Example -

Fetching the version number (2)


Output -

Вам также может понравиться