Working with assemblies in C# .net
- A DLL file is called as an Assembly.
- Assembly contains reusable code in the format of byte code.
- A collection of classes is called namespace
- Collection on namespaces is assembly
- Collection of Assemblies is FCL (Frame work class Library)
- Assembly supports language interoperability
That is the assembly developed in C #.net can be used in Vb .net and ASP.net also and vice versa.
- In some cases C #.net assembly may not work in VB.Net
- To add an assembly
Go to project menu Add reference Browse choose a DLL file
- To import the classes of name space “using” keyword is required .
- DLL System. DATA System. DATA. OLEDB
- Assemblies are divided in to two types
- In private assemblies all the referred DLLS will be copied automatically in to LOCAL projects debug folder.
- In shared assemblies , the referred DLLS will not be copied in to LOCAL project debug folder.
- Private assemblies occupies more memory , when these are referred from many projects.
- When many projects to be developed for the same client, then shared assemblies are recommended. if only one project is required for a client , then private assemblies are best.
Example on Private Assembly:-
Note: - To develop assemblies .net introduced class library project template
- Open a class library project with project
- Private test (out put will be private test. dll)
DLL files are not executable, only it contains reusable code.
For every project by default a namespace will be created with the name of project.
Namespace private e test
Public class test
Public string M1 ()
Return “form M1”;
Name space sub private Test
Public class test 2
Public String Print ()
Return “from print”;
Public string PRINT ()
Return “from PRINT”;
}//sub private test
Build the project (build menu build solution)
Private test . DLL is created under
D:/C194 /private test / bin / bebug folder with the following Structure
Similar to System . Io
- Calling private Test . DLL from a C #.net windows Application Project
- Open WFAP with project name c Test (C test or any name)
- Go to project menu Add reference Browse private Test .dll
- Place a button
Using private test; both should be return if one write,
Using private test. Sub private Test; then only that namespace classes all user
// code for button
Test t = new test ();
Message Box . show (t. M1());
Test 2 S = new Test 2 ();
Message Box . show (S. Print ());
Message Box . show (S. Print ());
Execute the Project (f5):-
When project is executed then private Test. DLL is copied in to C :/c 194/Ctest/Bin/debug folder, hence private Test . DLL is a private assembly.
- Calling Private Test.Dll from VB.net
- Windows Application Project
open vb.net windows Application Project with name V Test.
Project Menu Add reference Browse
Private Test. DLL
Place a button
Imports Private Test
Imports Private Test. Sub Private Test
//code for b1_click
Dim t As New Test
Message Box (t1. M1())
Dim S As New Test2
Message Box (S. print ())//cannot be called.
Test 2 is having two methods with name print and PRINT where only case is a different . This type of methods are not accessible from VB.net because VB.net is not case sensitive.
Working with Shared Assemblies:-
- ALL DLL file which is registered with GAC is called as shared Assembly.
GAC – Global Assembly cache
- C:/windows/Assembly folder is called as GAC.
- Assembly folder a secured folder where copy and paste is not allowed.
- A DLL, which contains a strong name can be registered with GAC.
- Strong name is also called as public key token, which provides unique identifier, to as assembly.
- Syntax to create Strong name
Sn – K abc.snk
- A DLL with strong name can be registered with GAC with the help of GacUtil tool.
Gacutil -I dll file
Example on shared Assembly:-
- Open class library project with project name asc 123
Name space ABC 123
Public Class Test
Public String Get Test ()
Return “from get text”;
Steps to create a Strong name in C# .net
- Open .net command prompt
(Start Programs Microsoft visual Studio 2008 Visual Studio Tools visual Studio 2008 command prompt )
D: enter key
- Cd c 1194
- C 194 > cd abs 123
Abc 123 >cd bin
Abc 123/bin > cd debug
Abc.snk file is created with a strong name linking abc.snk in the current project
Project menu Abc 123 Properties Signing check “Sign the assembly” check box Select “” “browse” from the combo box choose ABC.snk file
Providing Meta Information (optional):
Project menu ABC 123 Properties Application click on Assembly Information button and type some information.
Build the project (build build Solution)
ABC 123. DLL is created under
D:/c194 /ABC 123 / Bin /Debug folder
Open .net command prompt and type as follows
Gacutil – I abc 123.dll
ABC 123. DLL is registered with GAC
(C :/windows/Assembly) hence it is called as shared Assembly.
Open windows Forms Application Project with Project name WINTEST
Project menu Add reference Browse Abc 123.dll
Place a Button
Using ABC 123
Name space WINTEST
Code for Button1_click
Test t = new Test ();
Message Box. Show (t. Get Text ());
Execute the Project (f5)
ABC123.DLL is not copied in to
D :/ c194/WINTEST/bin /Debug folder, hence Abc 123.DLL is a Shared Assembly
All predefined assemblies are shared assemblies