Friday, 13 January 2012

Assembly Types | VB.Net Tutorial For Biginners PDF Download

    Assemblies can be single file assemblies or multi file assemblies. In multi file assemblies one of the files must contain the assembly’s manifest data. Multi file assemblies can have only one entry point even though the assembly can contain multiple code modules. A multi file assembly is created primarily for combining modules written in different programming languages. Once the assembly is created, the file that contains the assembly manifest (and hence the assembly) can be signed, or one can give the file (and the assembly) a strong name and put it in the global assembly cache.
     Main uses of multi file assembly are for combining modules written in different programming languages. They enable optimization of downloading an application by putting seldom-used types in a module that is downloaded only when needed. The .NET Framework downloads a file only when it is referenced; keeping infrequently referenced code in a separate file from the application optimizes code download.
   Let us look at an example of how to create multi file assembly.

AddModule.cs
Copy and Paste the following code into Notepad and save it as or AddModule.vb, depending on the language that you are using
Code Listing in C#
using System;
public class AddClass
{
public int Add(int Operand1, int Operand2)
{
return Operand1 + Operand2;
}
}
Compiling in C#
/Compilation csc /r:System.dll /t:Module AddModule.cs
Code Listing in VB.NET
Imports System
Public Module AddModule
Public Class AddClass
Function Add(ByVal Operand1 As Integer, ByVal Operand2 As Integer) As Integer
Add = Operand1 + Operand2
End Function
End Class
End Module
Compiling in VB.NET
vbc /r:System.dll /t:Module AddModule.vb
This file is compiled with the target option as module. Hence an assembly is not created.
The output is a file with an extension of .netmodule.
Similarly create SubtractModule.cs/ as shown below
SubtractModule.vb
Code Listing in C#
using System;
public class SubtractClass
{
public int Subtract(int Operand1 , int Operand2 )
{
return Operand1 - Operand2;
}
}
Compiling in C#
csc /r:System.dll /t:Module SubtractModule.cs
Code Listing in VB.NET
Imports System
Public Module SubtractModule
Public Class SubtractClass
Function Subtract(ByVal Operand1 As Integer, ByVal Operand2 As Integer) As
Integer
Subtract = Operand1 - Operand2
End Function
End Class
End Module
Compiling in VB.NET
vbc /r:System.dll /t:Module SubtractModule.vb
Now create the main module, which references the above modules. The code is as shown
below for MainModule.cs/MainModule.vb
MainModule.cs
Code Listing in C#
using System;
public class MainModule
{
public static void Main()
{
int iOperand1, iOperand2, iResult ;
iOperand1 = 22;
iOperand2 = 11;
iResult = 0;
AddClass objAddClass = New AddClass();
SubtractClass objSubtractClass = New SubtractClass();
iResult = objAddClass.Add(iOperand1, iOperand2);
Console.WriteLine(iResult.ToString());
iResult = objSubtractClass.Subtract(iOperand1, iOperand2);
Console.WriteLine(iResult.ToString());
Console.ReadLine();
}
}
Compiling in C#
Compilation csc /r:System.dll MainModule.cs
Code Listing in VB.NET
Imports System
Public Module MainModule
Sub Main()
Dim iOperand1, iOperand2, iResult As Integer
iOperand1 = 22
iOperand2 = 11
iResult = 0
Dim objAddClass As New AddClass
Dim objSubtractClass As New SubtractClass
iResult = objAddClass.Add(iOperand1, iOperand2)
Console.WriteLine(iResult.ToString)
iResult = objSubtractClass.Subtract(iOperand1, iOperand2)
Console.WriteLine(iResult.ToString)
Console.ReadLine()
End Sub
End Module
Compiling in VB.NET
vbc /r:System.dll MainModule.vb
The code is compiled as follows
To create a multi file assemble, which contains the two modules created previously
namely AddModule and SubtractModule, compile using the following command at the
command prompt
Compiling in C#
csc /System.dll /addModule:AddModule.netmodule
/addModule:SubtractModule.netmodule MainModule.cs
Compiling in VB.NET
Vbc /System.dll /addModule:AddModule.netmodule
/addModule:SubtractModule.netmodule MainModule.vb
This process creates a multi file assembly. This assembly contains the two modules
created previously namely AddModule and SubtractModule. Thus this assembly contains
multiple modules. If ildasm utility is executed on the MainModule assembly, it shows
that the manifest information in the MainModule contains references to the AddModule
and the SubtractModule modules. That is the modules are linked to the main assembly by
the information contained in the main assembly’s manifest information.