NET Framework C n NET Framework Problem background
. NET Framework & C#
n . NET Framework ¨ Problem background ¨ Solution ¨ Common n Language Runtime (CLR) MS Intermediate Language ¨ MSIL Structure ¨ MSIL and Java bytecode n Introduction to C# language ¨ C# Structure ¨ C# Features
Problems Background n n n n n Good code is hard to write All system features in any language COM problems Platform Interoperability Automatic memory management Object-Oriented features in and between all PLs Safety & Security Better Access to OS functions Interoperate with COM (both as client & server)
Solution (Common Language Runtime) Managed Code in Common Language Runtime Managed Code Common Language Runtime Requests for existing feature Win 32 OS New CLR-Only Feature
Solution (Microsoft Intermediate Language) Different Languages are Compiled into MSIL Source code : Any languages Development tools Just-In-Time MS Intermediate Language compiler Platform specific code
. Net platform features Automatic memory management n Explicit Versioning n OO features in any languages n Accessing system functionality throw a hierarchical namespace n Code security n Interoperability with COM n
Costs: OS got harder to write n More memory and CPU time n
CLR structure
Compare with traditional ways C# VB C++ J-script other Compiler Intermediate Language C++ Compiler Executable Existing OS JIT econo. JIT Executables. NET CLR Machine hardware Pre. JIT
A simple example
Time. Component. vb Imports Microsoft. Visual. Basic ' Declare the namespace that clients will use to access ' the classes in this component Namespace Time. Component. NS Public Class Time. Component Public Function Get. Time(By. Val Show. Seconds As Boolean) As String If (Show. Seconds = True) Then Return Now. To. Long. Time. String Else Return Now. To. Short. Time. String End If End Function End Class End Namespace
Timeclient. cs using System ; using Time. Component. NS ; class Main. App { public static void Main() { // Declare and create a new component of the class // provided by the VB server we wrote Time. Component tc = new Time. Component ( ) ; // Call the server's Get. Time method. Write its // resulting string to a console window. Console. Write (tc. Get. Time (true)) ; } }
Assembly A logical collection of one or more exe and dll file containing an application’s code and resources It contains 1 -Codes in MSIL 2 -Manifest A metadata description of the code and resources
Metadata n n n What classes and method it contains What external objects it requires What version of code it represents
Manifest example (Time. Component. dll). assembly extern mscorlib }. publickeytoken = (B 7 7 A 5 C 56 19 34 E 0 89 ). ver 1: 0: 2411: 0 {. assembly extern Microsoft. Visual. Basic }. publickeytoken = (B 0 3 F 5 F 7 F 11 D 5 0 A 3 A: . . _? . //. ver 7: 0: 0: 0 {. assembly Time. Component }. hash algorithm 0 x 00008004. ver 0: 0: 0: 0 {. module Time. Component. dll //MVID: {144 ACC 38 -E 825 -45 C 4 -83 A 6 -C 2 A 9 E 5 A 901 DD{. imagebase 0 x 00400000. subsystem 0 x 00000002. file alignment 512. corflags 0 x 00000001 //Image base: 0 x 032 a 0000 //. zV. 4. . (
Private or Public q Private In client directory q Public (shared) In Global Assembly Cache (GAC) \winntassembly
Namespaces A logical division within which a name need to be unique n The best way to handle a large list of system objects and functions n System namespace (implemented in several separated DLLs n We can import a namespace n Your code can have its own namespace n
Versioning DLL Hell Replacing a DLL used by an existing client, with a newer version (or vice versa) q. NET provides a standard way to specify version q Each client assembly uses its own specified versions of related assemblies
Object oriented programming Whether to smarten up non-object oriented languages or dump down OO languages n. NET provides all languages with inheritance and constructors features n Virtual Object Model n
n All. NET classes inherit from the base class System. Object Ø Equals Ø Get. Hash. Code Ø Get. Type Ø To. String
Cross-language inheritance Because of the standardized IL architecture you can inherit form a class in another language For example: System. Object
. NET memory management q. NET CLR provides all languages with automatic memory management. q Garbage collector automatically removes unreferenced objects q You can force a garbage collection manually
Interoperability with COM objects q. NET Supports interoperation with COM object as client or server via a runtime callable wrapper or a COM Callable Wrapper
Using COM objects from. NET objects
Using. NET objects from COM objects
Transaction in. NET Transaction ensure the integrity of databases during complex operations n Native. NET objects can also participate in transactions with a simple attribute n
Structured exception handling. NET provides structured exception handling as a fundamental feature available in and between all languages n The exception handler can tell exactly where the exception originated by using a stack trace n
Code access security n n Most software comes from web A user doesn’t know whether a Web code is safe or not. NET allows an administrator to specify privileges that each managed code has. You can specify these privileges in three level by modifying XML-based files Enterprise Machine User \winntmicrosoft. netframework[c. v. ]config Ø Ø Ø
- Slides: 28