You are on page 1of 30

UNIT V

Defining the .NET Framework


The .NET Framework is
A software development environment A runtime engine for Managed Code A platform designed for Internet-Distributed software

The .NET Framework is an exciting new computing platform

What is the .NET Framework


A new computing platform designed to simplify application development A consistent object-oriented programming environment A code-execution environment that:
Minimizes software deployment and versioning conflicts Eliminates the performance problems of scripted or interpreted environments

Primary Components of .NET


.NET Framework Class Libraries
Object-oriented collection of reusable types Sits on-top of the Common Language Runtime

Common Language Runtime (CLR)


Manages code execution at runtime Memory management, thread management, etc. Code designed for the CLR is referred to as Managed Code

Inside the .NET Framework


VB C++ C# J# Python Web Services User Interface

ASP.NET
High-productivity environment for building and running Web services

ASP.NET Data and XML Base Framework Common Language Runtime (CLR)
Operating System

Secure, integrated class libraries


Unifies programming models across languages Enables cross-language integration Factored for extensibility Designed for tools

Common Language Runtime


Executes code, maintains security, handles component plumbing and dependencies

One Runtime For Many Languages


CLR is an open standard Any language can make use of CLR services Any language can use classes written in any other language Any language can inherit classes written in any other language

.NET Framework Class Libraries


Sit on top of the CLR Reusable types that tightly integrate with the CLR Object oriented inheritance, polymorphism, etc. Provide functionality for ASP.NET, XML Web Services, ADO.NET, Windows Forms, basic system functionality (IO, XML, etc.)

Assembly
An Assembly is the managed equivalent of an EXE/DLL Implements and optionally exports a collection of types It is the unit of versioning, security, and deployment Parts of an Assembly file
Windows PE header CLR header (Information interpreted by the CLR and utilities) Metadata (Type definition and reference tables) Intermediate Language (code emitted by compiler)

The Common Language Runtime


Code that targets the CLR is referred to as managed code All managed code has the features of the CLR
Object Oriented Cross-language integration Cross language exception handling Multiple version support (no more DLL Hell)

The Common Language Runtime


The CLR manages object layout and references to objects Objects whose lifetimes are managed by the CLR are referred to as Managed Data Automatic memory management reduces memory leaks In managed code you can use:
Managed Data Unmanaged data Both

Internet Distributed Software


Second-Tier
(database or other server)

Second-Tier

Second-Tier

Web Server

Web Server

Client

Client

Managed Code and the CLR


The Common Language Runtime (CLR) is a runtime engine
Manages .NET Code (such as C# applications) Provides features such as memory management, thread management, object type safety, security, etc. Is a part of the .NET Framework

Managed code
Code that targets the CLR Any .NET Language, including C#, Visual Basic, C++, Java, Cobol, etc.

The CLR and Managed Code


Managed Executable Legacy Software
(unmanaged code)
Reusable Managed Components

Common Language Runtime


(JIT compilation, memory management, etc.)

Windows
(or other operating oystem)

IL and Metadata
All Managed executables consist of
Intermediate Language (IL) instructions Metadata

IL
CPU independent machine language Just-in-time compiled at runtime

Metadata
Structured information describes programming constructs including
Classes definitions, field and method definitions, parameter lists, return types, etc.

From Source Code to Managed .exe


SomeSource.cs
using System.Windows.Forms; using System.Drawing; class MyForm:Form{ public static void Main(){ Application.Run( new MyForm()); } protected override void OnPaint( PaintEventArgs e){ e.Graphics.DrawString( "Hello World!", new Font("Arial",35), Brushes.Blue, 10, 100);

C# Compiler

SomeSources.exe Metadata

A Managed Application

IL

Just-in-Time Compiling
All managed code runs in native machine language However, all managed code is made up of IL and metadata The CLR JIT-compiles the IL and metadata
At execution time Executed directly by CPU

Allows for the best of both worlds


Code management features Performance of full-speed execution

Executing a Managed Application


SomeSources.exe Metadata At execution time the IL and Metadata are JIT compiled

Running Process Memory

IL

JIT Compiler Native Machine Language


10010100 10110000 10000000 10111010 11011011 11010111 11000010 01110110

The CPU executes the JITcompiled machine code directly

Garbage Collection
class MyClass{ void Method(){ Variable v1; Variable v2; do{ . . .

The Managed Heap


B A E D

Objects A and D will be cleaned up because neither is directly or indirectly referenced by code

= Object or buffer in memory

Remoting
NET Remoting provides a way for application in different machines/domains to communicate with each other. Remoting provides a powerful yet an easy way to communicate with object in different app domains. Any object which executes outside the app domain can be considered as Remote.

Remote objects are accessed via Channels, Channels can be thought of as Transport mechanism to pass messages to and from remote objects. All the method calls along with the parameters are passed to the remote object thru the channel viz. HTTP or TCP.

Understanding the How

Marshaling
Marshaling is the act of taking data from the environment you are in and exporting it to another environment. In the context of .NET, marhsaling refers to moving data outside of the app-domain you are in, somewhere else.

Intermediate Language
All .NET compilers produce IL code IL is CPU-independent machine language
Created by Microsoft with input from external commercial and academic language/compiler writers

IL is higher-level than most CPU machine languages Some sample IL instructions


Create and initialize objects (including arrays) Call virtual methods Throw and catch exceptions Store/load values to/from fields, parameters, and local variables

Developers can write in IL assembler (ILAsm.exe)


Many compilers produce IL source code and compile it by spawning ILAsm.exe

Executing Managed IL Code


When loaded, the runtime creates method stubs When a method is called, the stub jumps to runtime Runtime loads IL and compiles it
IL is compiled into native CPU code Just like compiler back-end

Method stub is removed and points to compiled code Compiled code is executed In future, when method is called, it just runs

Managed EXE
static void Main() { Console.WriteLine(Hello); Console.WriteLine(Goodbye); }

Console
static void WriteLine()

JITCompiler
static void WriteLine(String)

NativeMethod JITCompiler
(remaining members)

Native CPU Instructions

MSCorEE.dll
JITCompiler function { 1. In the assembly that implements the type (Console), look up the method (WriteLine) being called in the metadata. 2. From the metadata, get the IL for this method and verify it. 3. Allocate a block of memory. 4. Compile the IL into native CPU instructions; the native code is saved in the memory allocated in step #3. 5. Modify the methods entry in the Types table so that it now points to the memory block allocated in step #3. 6. Jump to the native code contained inside the memory block. }

All Types/Modules Are Self-Describing


public class App { public static void Main() { System.Console.WriteLine("Hi"); } }

1 TypeDef entry for App


Entry refers to MethodDef entry for Main

2 TypeRef entries for System.Object and System.Console


Both entries refer to AssemblyRef entry for MSCorLib

Metadata Definition Tables (Partial List)


TypeDef: 1 entry for each type defined
Types name, base type, flags (i.e. public, private, etc.) and index into MethodDef & FieldDef tables

MethodDef: 1 entry for each method defined


Methods name, flags (private, public, virtual, static, etc), IL offset, and index to ParamDef table

FieldDef: 1 entry for each field defined


Name, flags (i.e. private, public, etc.), and type

ParamDef: 1 entry for each parameter defd


Name, and flags (in, out, retval, etc.)

Metadata Reference Tables (Partial List)


AssemblyRef: 1 entry for each assembly refd
Name, version, culture, public key token

TypeRef: 1 entry for each type refd


Types name, and index into AssemblyRef table

MemberRef: 1 entry for each member refd


Name, signature, and index into TypeRef table

Code Attempts To Access A Type/Method


.method /*06000001*/ public hidebysig static void Main(class System.String[] args) il managed { .entrypoint // Code size 11 (0xb) .maxstack 8 IL_0000: ldstr "Hi IL_0005: call void ['mscorlib'/* 23000001 */] System.Console/* 01000003 */::WriteLine(class System.String) IL_000a: ret } // end of method 'App::Main'

23000001: AssemblyRef entry for MSCorLib 01000003: TypeRef entry to System.Console 06000001: MethodDef for Main (FYI)

How The CLR Resolves An Assembly Reference


IL Call with Metadata token MethodDef MemberRef MemberRef TypeRef TypeRef AssemblyRef Load Assembly

MethodDef TypeDef

Create internal type structure

Look-up TypeDef

Emit Native Call

You might also like