Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 9 S. NandaGopalan, BIT

Similar presentations


Presentation on theme: "Chapter 9 S. NandaGopalan, BIT"— Presentation transcript:

1 Chapter 9 S. NandaGopalan, BIT
.NET Assemblies Chapter 9 S. NandaGopalan, BIT

2 Chapter 9 S. NandaGopalan, BIT
Objectives Overview of .NET Assemblies Building Single File Assembly Cross Language Inheritance Building Multifile Assembly Private Assembly Shared Assembly (GAC) Building Shared Assemblies Strong Names (sn) Chapter 9 S. NandaGopalan, BIT

3 Overview of .NET Assemblies
An Assembly is nothing but a versioned, self describing binary (*.dll or *.exe) Contains classes, interfaces, structures. Also some optional resources like images, string tables, etc.) Assemblies help in code reuse The format of .Net binaries are completely new Chapter 9 S. NandaGopalan, BIT

4 Elements of .NET Binaries
Windows file header CLR header CIL code Metadata Assembly Manifest Chapter 9 S. NandaGopalan, BIT

5 Single File Assembly Format
Foo.dll Manifest Metadata CIL Code Resources Chapter 9 S. NandaGopalan, BIT

6 Format of Multifile Assembly
Bar.netmodule Foo.dll Metadata Manifest Metadata CIL Code CIL Code Qazz.netmodule Metadata CIL Code CLogo.bmp Chapter 9 S. NandaGopalan, BIT

7 Building Single File Assembly
Step 1: Open the new Project Window. Select Visual C# Projects -> Class Library. Name this project as BankLibrary Chapter 9 S. NandaGopalan, BIT

8 Chapter 9 S. NandaGopalan, BIT
BankLibrary… Step 2: Type the code for BankLibrary with the following details: Account – Class Deposit() – to deposit the amount Withdraw() – to withdraw the amount Balance – property balance – member data Refer to 'BankLibrary' Folder for the code Chapter 9 S. NandaGopalan, BIT

9 Chapter 9 S. NandaGopalan, BIT
Subclass : SBAccount Step 3: Select Project -> Add Class.. and name the class as SBAccount. Step 4: Select Project -> Add Reference -> System.Windows.Forms.dll. Press Select button. Step 4: Add the following line in the program: using System.Windows.Forms; Chapter 9 S. NandaGopalan, BIT

10 Chapter 9 S. NandaGopalan, BIT
C# Client Application How to make use of our BankLibrary type for any client? We shall show first for a C# client. Follow the steps as: Step 1: Open a C# Console Application. Name it as BankClient Step 2: Press Project->Add Reference. Press Browse button and click on the appropriate BankLibrary.dll file of BankLibrary project. Step 3: Add the following line in the code: using BankLibrary; Step 4: Type the code as given in the file BankClient.cs. Chapter 9 S. NandaGopalan, BIT

11 Chapter 9 S. NandaGopalan, BIT
Code and Output static void Main(string[] args) { SBAccount sb = new SBAccount(); sb.Deposit(1000); sb.showBalance(); } Chapter 9 S. NandaGopalan, BIT

12 Chapter 9 S. NandaGopalan, BIT
VB .NET Client Add a new VB .NET Project called VBClient (windows application) Chapter 9 S. NandaGopalan, BIT

13 Chapter 9 S. NandaGopalan, BIT
VB .NET Client Step 2: Add the following line: Imports BankLibrary Step 3: Create a Button and name it as Balance Step 4: Type the code for this button (double click) Chapter 9 S. NandaGopalan, BIT

14 Chapter 9 S. NandaGopalan, BIT
Code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sb As New SBAccount sb.Deposit(400) sb.showBalance() sb.Withdraw(100) End Sub Chapter 9 S. NandaGopalan, BIT

15 Cross Language Inheritance
Can we create a subclass of Account in VB .NET? ……… Yes! But how? Step 1: In the same project of VBClient, add a class called CAAccount Press Project->Add Class and select class icon with name of the class as CAAccount Step 2: Add the following lines in the code: Public Class CAAccount Inherits BankLibrary.Account Public Overloads Sub showBalance() MessageBox.Show("It is a Current Account") End Sub End Class Step 3: Create a new Button on the same form called 'Current Account'. Double this button to add the following code Dim ca As New CAAccount ca.showBalance() Chapter 9 S. NandaGopalan, BIT

16 Chapter 9 S. NandaGopalan, BIT
Multifile Assembly Left as an Exercise……….. Chapter 9 S. NandaGopalan, BIT

17 Chapter 9 S. NandaGopalan, BIT
Shared Assemblies Shared assemblies can be used by several clients on a single machine For example, System.Windows.Forms.dll is deployed as a shared assembly. This is why any application can set a reference to this dll. private assemblies must be kept in the same directory as that of the application and this need not be true for shared assemblies All shared assembly files are kept in C:\WINDOWS\assembly These assemblies can use machine-wide applications: Global Assembly Cache (GAC) Chapter 9 S. NandaGopalan, BIT

18 Chapter 9 S. NandaGopalan, BIT
Steps to add in GAC Step 1: An Assembly should be assigned with a strong name (sn) Strong Name is used to uniquely identify the publisher of a given .NET binary Strong Name string name of the assembly version number public key culture identity Chapter 9 S. NandaGopalan, BIT

19 Building Shared Assembly
Step 2: Now create a new C# ClassLibrary named as SharedAssembly Step 3: Type the code as shown in SharedAssembly folder and compile Step 4: Create the strong key Chapter 9 S. NandaGopalan, BIT

20 Class Library for Std Class
using System; using System.Windows.Forms; public class Std { private int USN; public Std() MessageBox.Show("using Version "); } public void Dummy() MessageBox.Show("Calling Dummy method"); public int usn get { return USN; } set { USN = value; } Chapter 9 S. NandaGopalan, BIT

21 Building Shared Assembly…
Step 5: We must inform this snk to C# compiler where the *.snk file is located to record the public key in the assembly manifest Step 6: Open the AssemblyInfo.cs and modify the file as shown below: update this line [assembly: AssemblyKeyFile("")] Specify the directory path of *.snk file [assembly: and Settings\SNG\myKey.snk>")] Step 7: Change the following line [assembly: AssemblyVersion("1.0.*")] to [assembly: AssemblyVersion(" ")] Chapter 9 S. NandaGopalan, BIT

22 Building Shared Assembly…
Step 8: Type the following code after opening a new C# Console Application project named as 'TestSharedAssembly' using SharedAssembly; class TestSharedAssembly { static void Main(string[] args) Std s = new Std(); s.Dummy(); } Chapter 9 S. NandaGopalan, BIT

23 Chapter 9 S. NandaGopalan, BIT
Versioning the dll Step 1: Update your class by adding new functionality or modify the existing class Step -2: Relocate your old dll file with version Step 3: Change the AssemblyInfo.cs file to reflect the new version: Step 4: Compile your *.cs file. Now you will see two binaries with the same name in GAC (however, they are different versions) – try this………! Chapter 9 S. NandaGopalan, BIT

24 Chapter 9 S. NandaGopalan, BIT
End of Chapter 9 Chapter 9 S. NandaGopalan, BIT


Download ppt "Chapter 9 S. NandaGopalan, BIT"

Similar presentations


Ads by Google