INF230 Basics in C# Programming AUBG, COS dept Lecture 29 Title: DLL libraries, ILDASM (part 1) Reference: Doyle, chap 11
Chapter 11 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program Design 4th Edition
Lecture Contents: OOP Features (reminder) Advanced OOP features. Abstraction , Encapsulation, Inheritance, Polymorphism, Generics Advanced OOP features. Component-Based Development Multi tier apps – Business, Presentation, Data layers, components Using libraries DLL to implement components
Object-Oriented Language Features Abstraction Generalizing, identifying essential features, hiding nonessential complexities Encapsulation Packaging data and behaviors into a single unit, hiding implementation details Inheritance Extending program units to enable reuse of code Polymorphism Providing multiple (different) implementations of same named behaviors C# Programming: From Problem Analysis to Program Design
Component-Based Development Figure 11-1 Component-based development C# Programming: From Problem Analysis to Program Design
Component-Based Development (continued) Component-based applications associated with multitier applications – (different layers) Business layer(s) – classes that perform processing Provide functionality for specific system; provide necessary processing Data access layer - classes for accessing data from text files and databases Presentation layer - User interface tier for user interaction Graphical User Interface such as Windows or Web C# Programming: From Problem Analysis to Program Design
Component-Based Development (continued) Components facilitates reuse-based approach Define and implement independent components into systems Components are implemented through classes Takes the form of objects or collection of objects Components can then be packaged with different components and used for other applications Classes can be created that extend functionality of original classes C# Programming: From Problem Analysis to Program Design
Component-Based Development (continued) SW products structure classification Logical structure Multi tier organization – UI layer, business layer, DB access layer Physical structure All components in sole package/module/file: NO SW reusability Components saved in a container prepared for reusability in many applications Most often container is organized as a library – static (.lib) or dynamic (.dll) C# Programming: From Problem Analysis to Program Design
DLL and ILDASM THE NEED of DLL as container of reusable code fragments (present lecture) The need of program tools to explore code fragments (next lecture)
Run Demo programs on DLL
Details on Compilation The C# compiler compiles source code (as a set of files with .cs as an extension) into an assembly. An assembly is the unit of packaging and deployment in .NET. An assembly can be either an application or a library. A normal console or Windows application has a Main() method and is an .exe file. A library is a .dll file and is equivalent to .exe without an entry point. Its purpose is to be called upon (referenced) by an application or by other libraries. C# Programming: From Problem Analysis to Program Design
Creating DLL
Dynamic Link Library (DLL) Several alternate options for creating components One option – compile source code into .dll file type Once you have DLL, any application that wants to use it just adds a reference to the DLL That referenced file with the .dll extension becomes part of the application’s private assembly First way: Can use the command line to create DLL C# Programming: From Problem Analysis to Program Design
A. Using command line to create DLL files To create DLL from MS VS cmd prompt. You get it: Start>All Programs>MS VSxx>VSTools>Developer Cmd prompt for VSxx At the cmd prompt you should type cmd like this: csc /target:library /out:Counter.dll Counter.cs You can specify more input files csc /target:library /out:Counter.dll Counter.cs P1.cs P2.cs Option /target:library makes compiler to generate .dll instead .exe Option /out may omit. Then output file is named after first listed source C# Programming: From Problem Analysis to Program Design
Dynamic Link Library (DLL) Using command line to create DLL files csc /target:exe build a console executable csc /target:winexe build Windows executable csc /target:library build a library csc /target:module build a module that can be added to another assembly C# Programming: From Problem Analysis to Program Design
B. Using VS to create DLL files Second way: more user friendly and attractive from within VS. You should select Class Library template for a project C# Programming: From Problem Analysis to Program Design
Building instead of Running Project The source code that you type includes one or more class definitions. These classes have no Main() method within them. To compile and create DLL, you select options under Build menu: Build > Build Solution Build > ReBuild Solution C# Programming: From Problem Analysis to Program Design
Client App to use the DLL Creating Client App to use the DLL
Creating a Client App to use DLL The created DLL can be reused with many different apps. You need to do two steps to use the DLL First: to add a reference to the components in your program Second: to include using directive with the appropriate namespace C# Programming: From Problem Analysis to Program Design
Sample Demo Projects INF230CreateLibrary INF230UsePrivateLibrary The project name identifies the library name, i.e. this project creates file INF230CreateLibrary.dll INF230UsePrivateLibrary The client project needs to include: using INF230CreateLibrary; // namespace directive To add a reference from within the project to the newly created user dll C# Programming: From Problem Analysis to Program Design
Sample Demo Projects DLLCreateClassLibrary DLLUseClassLibrary The project name identifies the library name, i.e. this project creates file DLLCreateClassLibrary.dll DLLUseClassLibrary The client project needs to include: using DLLCreateClassLibrary; // namespace directive To add a reference from within the project to the newly created user dll C# Programming: From Problem Analysis to Program Design
Build Instead of Run to Create DLL Create the parent class first in order to use IntelliSense (for sub classes) To illustrate creating stand-alone components, separate project created for Person and Student Because Visual Studio assigns the namespace name the same name as the project name (Person), you should change the namespace identifier If you don’t change it, when you start adding a reference to a created DLL, it can become confusing Changed to PersonNamespace for the example C# Programming: From Problem Analysis to Program Design
Build Instead of Run to Create DLL After typing class, do not run it – select BUILD, Build Solution Figure 11-7 Attempting to run a class library file Create new project for subclasses (Student) Again, select Class Library template from Start page Change namespace identifier (StudentNamespace) C# Programming: From Problem Analysis to Program Design
Build Instead of Run to Create DLL (continued) Add a new using statement to subclass In Student, add using PersonNamespace; To gain access to base class members in subclass, Add Reference in subclass to base class Use Solution Explorer window DLL is stored in Debug directory under the bin folder wherever you create the project C# Programming: From Problem Analysis to Program Design
Add Reference to Base Class One of the first things to do is Add a Reference to the Parent DLL Figure 11-8 Adding a reference to a DLL C# Programming: From Problem Analysis to Program Design
Add Reference to Base Class (continued) Use Browse button to locate DLL Figure 11-9 Add Reference dialog box C# Programming: From Problem Analysis to Program Design
Add Reference to Base Class (continued) Figure 11-10 Locating the Person.dll component C# Programming: From Problem Analysis to Program Design
Adding a New Using Statement In the subclass class, if you simply type the following, you receive an error message public class Student : Person Figure 11-11 Namespace reference error C# Programming: From Problem Analysis to Program Design
Adding a New Using Statement (continued) Notice fully qualified name To avoid error, could type: public class Student : PersonNamespace.Person Better option is to add a using directive using PersonNamespace; // Use whatever name you // typed for the namespace for Person After typing program statements, build the DLL from the Build option under the BUILD menu bar C# Programming: From Problem Analysis to Program Design
Creating a Client Application to Use the DLL DLL components can be reused with many different applications Once the component is in the library, any number of applications can reference it Instantiate objects of the referenced DLL Two steps Required Add a reference to the DLL components Include a using statement with the namespace name C# Programming: From Problem Analysis to Program Design
Creating a Client Application to Use the DLL (continued) Figure 11-12 DLLs referenced in the PresentationGUI class C# Programming: From Problem Analysis to Program Design
Practical session Creating DLL Creating a client app to use the DLL C# Programming: From Problem Analysis to Program Design
Classes as DLL contents Project ClassLibrary3 Contains class Counter definition Contains class Distance definition File ClassCountDistDefined.cs Project ClassLibrary3UsedBySB Contains references to Counter class Contains references to Distance class File ClassCountDistUsed.cs C# Programming: From Problem Analysis to Program Design
Classes as DLL contents Compile and run a pair of projects based on DLL ClassLibrary3 C# Programming: From Problem Analysis to Program Design
Structures as DLL contents Project ClassLibrary4 Contains struct Book definition File StructBookDefined.cs Project ClassLibrary4UsedBySB Contains references to Book struct File StructBookUsed.cs C# Programming: From Problem Analysis to Program Design
Structures as DLL contents Compile and run a pair of projects based on DLL ClassLibrary4 C# Programming: From Problem Analysis to Program Design
Interfaces as DLL contents Project ClassLiIbrary5 Contains interface ITraveler definition File ITravelerDefined.cs Project ClassLibrary5UsedBySB Contains references to Itraveler interface File ITravelerUsed.cs C# Programming: From Problem Analysis to Program Design
Interfaces as DLL contents Compile and run a pair of projects based on DLL ClassLibrary5 C# Programming: From Problem Analysis to Program Design
Thank You For Your Attention!
Using ILDASM to View the Assembly (ILDASM): Intermediate Language Disassembler tool Assembly shows the signatures of all methods, data fields, and properties One option – display the source code as a comment in the assembly Can be run from the command line or from within the Visual Studio IDE C# Programming: From Problem Analysis to Program Design
Using ILDASM to View the Assembly (continued) In order to use ILDASM in Visual Student, must be added as an external tool in Visual Studio (TOOLS, External Tools…) DLL files cannot be modified or viewed as source code Can use ILDASM to view DLL assemblies C# Programming: From Problem Analysis to Program Design
ILDASM to View the Assembly .ctors are constructors IL code for the method Data fields Properties converted to methods Figure 11-14 Student.dll assembly from ILDASM C# Programming: From Problem Analysis to Program Design
Thank You For Your Attention!