Professional Documents
Culture Documents
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
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.
Provides a physical Provides a logical structure to the names of structure to the runtime components of an source code elements executable
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
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
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
Assembly
Metadata
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
MySource.netmodule y Once a module is generated it can be made a part of any other assembly
assembly
IL Disassembler
y This tool can be used to view the content of assemblies
and manifests
IL Disassembler (Cont .)
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
addr.resources
y The resgen utility can also create XML-based resource file (.resX
ResourceWriter Class
y Using the ResourceWriter class of the .NET
resources
y 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 .
y Using defining the scope at the end of which an
Picture Box
Label
Cont
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
Viewing GAC
y The files in the GAC can be viewed using
manipulate, delete and view assemblies y Can be used to install, uninstall and list assemblies on the GAC using the command line
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
name we have created y For this we go to the Solution Explorer and project properties y From left side select signing tap
take
Installing an Assembly
y To install this signed assembly on the GAC we will use
gacutil.exe
Versioning
y Versioning comes into picture when there is an
Version Compatibility
Change In Major Number or Minor Number Compatibility
Revision Number
Build Number
the assembly