Sie sind auf Seite 1von 25

Architecture of the .

NET Framework
1
Chapter 1
Architecture of the .NET Framework

Th e .NET Fr a m ewor k developm en t pla tfor m in tr odu ces m a n y n ew con cepts, tech n ologies, a n d ter m s. Th e goa l of th is
ch a pter is to give a n over view of th e .NET Fr a m ewor k: to sh ow h ow it is a r ch itected, to in tr odu ce som e of th e n ew
tech n ologies, a n d to defin e m a n y of th e n ew ter m s. I ll a lso ta ke you th r ou gh th e pr ocess of bu ildin g you r sou r ce code
in to a n a pplica tion or a set of r edistr ibu ta ble com pon en ts ( types) , a n d th en expla in h ow th ese com pon en ts execu te.
Compiling Source Code into Managed Modules
OK, so you ve decided to u se th e .NET Fr a m ewor k a s you r developm en t pla tfor m . Gr ea t! You r fir st step is to deter m in e
wh a t type of a pplica tion or com pon en t you in ten d to bu ild. Lets ju st a ssu m e th a t you ve h a n dled th is m in or deta il,
ever yth in g is design ed, th e specifica tion s a r e wr itten , a n d you r e r ea dy to sta r t developm en t.
Next, you m u st decide wh a t pr ogr a m m in g la n gu a ge to u se. Th is is u su a lly a difficu lt ta sk beca u se differ en t la n gu a ges
offer differ en t ca pa bilities. For exa m ple, in u n m a n a ged C/ C+ + , you h a ve pr etty low- level con tr ol of th e system . You ca n
m a n a ge m em or y exa ctly th e wa y you wa n t to, cr ea te th r ea ds ea sily if you n eed to, a n d so on . Visu a l Ba sic 6, on th e oth er
h a n d, a llows you to bu ild UI a pplica tion s ver y r a pidly a n d a llows th e ea sy con tr ol of COM objects a n d da ta ba ses.
I f you u se th e .NET Fr a m ewor k, you r code ta r gets th e com m on la n gu a ge r u n tim e ( CLR) , wh ich a ffects you r decision
a bou t a pr ogr a m m in g la n gu a ge. Th e com m on la n gu a ge r u n tim e is ju st wh a t its n a m e sa ys it is: A r u n tim e th a t is
u sa ble by differ en t a n d va r ied pr ogr a m m in g la n gu a ges. Th e fea tu r es of th e CLR a r e a va ila ble to a n y a n d a ll
pr ogr a m m in g la n gu a ges th a t ta r get it- per iod. I f th e r u n tim e u ses exception s to r epor t er r or s, th en a ll la n gu a ges get
er r or s r epor ted via exception s. I f th e r u n tim e a llows you to cr ea te a th r ea d, th en a n y la n gu a ge ca n cr ea te a th r ea d.
I n fa ct, a t r u n tim e, th e CLR h a s n o idea wh ich pr ogr a m m in g la n gu a ge th e developer u sed for th e sou r ce code. Th is
m ea n s th a t you sh ou ld ch oose wh a tever pr ogr a m m in g la n gu a ge a llows you to expr ess you r in ten tion s m ost ea sily. You
m a y develop you r code in a n y pr ogr a m m in g la n gu a ge you desir e a s lon g a s th e com piler you u se to com pile you r code
ta r gets th e CLR.
So if wh a t I sa y is tr u e, th en wh a t is th e a dva n ta ge of u sin g on e pr ogr a m m in g la n gu a ge over a n oth er ? Well, I th in k of
com piler s a s syn ta x ch ecker s a n d "cor r ect code" a n a lyzer s. Th ey exa m in e you r sou r ce code, en su r e th a t wh a tever you ve
wr itten m a kes som e sen se, a n d th en ou tpu t code th a t descr ibes you r in ten tion . Sim ply pu t, differ en t pr ogr a m m in g
la n gu a ges a llow you to develop u sin g differ en t syn ta x. Don t u n der estim a te th e va lu e of th is. For m a th em a tica l or
fin a n cia l a pplica tion s, expr essin g you r in ten tion s u sin g APL syn ta x ca n sa ve m a n y da ys of developm en t tim e wh en
com pa r ed to expr essin g th e sa m e in ten tion u sin g Per l syn ta x, for exa m ple.
Micr osoft is cr ea tin g sever a l la n gu a ge com piler s th a t ta r get th e r u n tim e: C+ + with m a n a ged exten sion s, C#
( pr on ou n ced C sh a r p ) ,

Visu a l Ba sic.NET, JScr ipt, Ja va , a n d a n in ter m edia te la n gu a ge ( I L) Assem bler . I n a ddition to
Micr osoft, th er e a r e sever a l oth er com pa n ies cr ea tin g com piler s th a t pr odu ce code th a t ta r gets th e CLR. At th is wr itin g, I
a m a wa r e of com piler s for Alice, APL, COBOL, Com pon en t Pa sca l, Eiffel, For tr a n , Ha skell, Mer cu r y, ML, Mon dr ia n ,
Ober on , Per l, Pyth on , RPG, Sch em e, a n d Sm a llta lk.
Th e figu re on th e n ext pa ge sh ows th e pr ocess of com pilin g sou r ce code files:
Applied .NET Framework Programming
2

As th e figu r e sh ows, you ca n cr ea te sou r ce code files u sin g a n y pr ogr a m m in g la n gu a ge th a t su ppor ts th e CLR. Th en , you
u se th e cor r espon din g com piler to ch eck syn ta x a n d a n a lyze th e sou r ce code. Rega r dless of wh ich com piler you u se, th e
r esu lt is a managed module. A m a n a ged m odu le is a sta n da r d Win dows por ta ble execu ta ble ( PE) file th a t r equ ir es th e
CLR to execu te. I n th e fu tu r e, oth er oper a tin g system s m a y u se th e PE file for m a t a s well.
A Ma n a ged Modu le is com posed of th e followin g pa r ts:
Part Description
PE h ea der Th is is th e sta n da r d Win dows PE file h ea der , wh ich is sim ila r to th e
Com m on Object File For m a t ( COFF) h ea der . Th e PE h ea der in dica tes th e
type of file- - GUI , CUI , or DLL a n d a lso h a s a tim esta m p in dica tin g wh en
th e file wa s bu ilt. For m odu les th a t con ta in on ly I L code ( see below,
I n ter m edia te La n gu a ge Code) , th e bu lk of th e in for m a tion in th e PE h ea der
is ign or ed. For m odu les th a t con ta in n a tive CPU code, th is h ea der con ta in s
in for m a tion a bou t th e n a tive CPU code.
CLR h ea der Th is h ea der con ta in s th e in for m a tion ( in ter pr eted by th e CLR a n d u tilities)
th a t m a kes th is a m a n a ged m odu le. I t in clu des th e ver sion of th e CLR
r equ ir ed, som e fla gs, th e Meth odDef m eta da ta token of th e m a n a ged
m odu les en tr y poin t m eth od ( Main m eth od) , a n d th e loca tion / size of th e
m odu les m eta da ta , r esou r ces, str on g n a m e, som e fla gs, a n d oth er less
in ter estin g stu ff.
Meta da ta Ever y m a n a ged m odu le con ta in s m eta da ta ta bles, of wh ich th er e a r e 2 m a in
types: th ose th a t descr ibe th e types a n d m em ber s defined inyou r sou r ce
code, a n d th ose th a t descr ibe th e types a n d m em ber s referenced byyou r
sou r ce code.
Architecture of the .NET Framework
3
Part Description
I n ter m edia te La n gu a ge ( I L) Code Th is is th e code th a t wa s pr odu ced by th e com piler a s it com piled th e sou r ce
code. I L is la ter com piled by th e CLR in to n a tive CPU in str u ction s.

Most com piler s of th e pa st pr odu ced code ta r geted to a specific CPU a r ch itectu r e, su ch a s x86, I A64, Alph a , or Power PC.
All CLR- com plia n t com piler s pr odu ce in ter m edia te la n gu a ge ( I L) code in stea d. I L code is som etim es r efer r ed to a s
m a n a ged code, beca u se its lifetim e a n d execu tion a r e m a n a ged by th e CLR. I L code is discu ssed la ter in th is ch a pter .
I n a ddition to em ittin g I L, ever y com piler ta r getin g th e CLR is r equ ir ed to em it fu ll m eta da ta in to ever y m a n a ged
m odu le. I n br ief, metadatais sim ply a set of da ta ta bles th a t descr ibe wh a t is defin ed in th e m odu le, su ch a s types a n d
th eir m em ber s. I n a ddition , m eta da ta a lso h a s ta bles in dica tin g wh a t th e m a n a ged m odu le r efer en ces, su ch a s im por ted
types a n d th eir m em ber s. Meta da ta is a su per set of older tech n ologies su ch a s type libr a r ies a n d I DL files. Th e im por ta n t
th in g to n ote is th a t CLR m eta da ta is fa r m or e com plete th a n its pr edecessor s. An d, u n like type libr a r ies a n d I DL,
m eta da ta is a lwa ys a ssocia ted with th e file th a t con ta in s th e I L code. I n fa ct, th e m eta da ta is a lwa ys em bedded in th e
sa m e EXE/ DLL a s th e code, m a kin g it im possible to sepa r a te th e two. Sin ce th e m eta da ta a n d code a r e pr odu ced by th e
com piler a t th e sa m e tim e a n d a r e bou n d in to th e r esu ltin g m a n a ged m odu le, th e m eta da ta a n d th e I L code it descr ibes
a r e n ever ou t of syn c with on e a n oth er .
Meta da ta h a s m a n y u ses. Her e a r e som e of th em :
Meta da ta r em oves th e n eed for h ea der a n d libr a r y files wh en com pilin g, beca u se a ll th e in for m a tion
a bou t th e r efer en ced types/ m em ber s is con ta in ed in on e file a lon g with th e I L th a t im plem en ts th ose
type/ m em ber s. Com piler s ca n r ea d m eta da ta dir ectly fr om m a n a ged m odu les.
Visu a l Stu dio u ses m eta da ta to h elp you wr ite code. I ts I n telliSen se fea tu r e pa r ses m eta da ta to tell you
wh a t m eth ods a type offer s a n d wh a t pa r a m eter s th a t m eth od expects.
Th e CLR code ver ifica tion pr ocess u ses m eta da ta to en su r e th a t you r code per for m s on ly sa fe
oper a tion s. Ver ifica tion is discu ssed sh or tly.
Meta da ta a llows a n objects fields to be ser ia lized in to a m em or y block, r em oted to a n oth er m a ch in e, a n d
th en deser ia lized, r ecr ea tin g th e object a n d its sta te on th e r em ote m a ch in e.
Meta da ta a llows th e ga r ba ge collector to tr a ck th e lifetim e of objects. For a n y object, th e ga r ba ge collector
ca n deter m in e th e type of th e object, a n d fr om th e m eta da ta it kn ows wh ich fields with in th a t object r efer
to oth er objects.
Th e n ext ch a pter , Bu ildin g, Pa cka gin g, Deployin g, a n d Adm in ister in g Applica tion s a n d Types, will descr ibe m eta da ta
in m u ch m or e deta il. An d a little la ter in th is ch a pter , well explor e in ter m edia te la n gu a ge in m or e deta il.
Fou r of th e com piler s th a t Micr osoft offer s- - C#, Visu a l Ba sic, JScr ipt, a n d th e I L Assem bler - - a lwa ys pr odu ce m a n a ged
m odu les, wh ich r equ ir e th e CLR to execu te. Th a t is, en d u ser s m u st h a ve th e CLR in sta lled on th eir m a ch in es in or der to
execu te a n y m a n a ged m odu les. Th is situ a tion is sim ila r to th e on e th a t en d u ser s fa ce with MFC or VB 6 a pplica tion s:
th ey m u st h a ve th e MFC or VB DLLs in sta lled in or der to r u n th em .
Th e Micr osoft C+ + com piler , by defa u lt, bu ilds u n m a n a ged m odu les: th e EXE or DLL files with wh ich we a r e a ll
fa m ilia r . Th ese m odu les do n ot r equ ir e th e CLR in or der to execu te. However , by specifyin g a n ew com m a n d- lin e switch ,
th e C+ + com piler ca n pr odu ce m a n a ged m odu les th a t do r equ ir e th e CLR to execu te. Of a ll th e Micr osoft com piler s
m en tion ed, C+ + is u n iqu e in th a t it is th e on ly la n gu a ge th a t a llows th e developer to wr ite both m a n a ged a n d
Applied .NET Framework Programming
4
u n m a n a ged code a n d h a ve it em itted in to a sin gle m a n a ged m odu le. Th is ca n be a gr ea t fea tu r e beca u se it a llows
developer s to wr ite th e bu lk of th eir a pplica tion s in m a n a ged code ( for type- sa fety a n d com pon en t in ter oper a bility) bu t
con tin u e to a ccess th eir existin g u n m a n a ged C+ + code.
Combining Managed Modules into Assemblies
Th e CLR doesn t a ctu a lly wor k with m odu les; it wor ks with a ssem blies. An assemblyis a n a bstr a ct con cept, wh ich ca n be
difficu lt to gr a sp a t fir st. Fir st, a n a ssem bly is a logica l gr ou pin g of on e or m or e m a n a ged m odu les or r esou r ce files.
Secon d, a n a ssem bly is th e sm a llest u n it of r eu se, secu r ity, a n d ver sion in g. Depen din g on th e ch oices you m a ke with
you r com piler s or tools, you ca n pr odu ce a sin gle- file a ssem bly or you ca n pr odu ce a m u lti- file a ssem bly.
Ch a pter 2, Bu ildin g, Pa cka gin g, Deployin g, a n d Adm in ister in g Applica tion s a n d Types, discu sses a ssem blies in gr ea t
deta il, so I don t wa n t to spen d a lot of tim e on it h er e. All I wa n t to do n ow is m a ke you a wa r e th a t th er e is th is extr a
con ceptu a l n otion th a t offer s a wa y to tr ea t a gr ou p of files a s a sin gle en tity.
Th e figu r e below sh ou ld h elp expla in wh a t a ssem blies a r e a bou t:

I n th is figu r e, we a r e pa ssin g th e file n a m es of som e m a n a ged m odu les a n d r esou r ce ( or da ta ) files to a tool. Th is tool
pr odu ces a sin gle PE file th a t r epr esen ts th e logica l gr ou pin g of files. Th is PE file con ta in s a block of da ta ca lled th e
manifest, wh ich is sim ply a n oth er set of m eta da ta ta bles. Th ese ta bles descr ibe th e a ssem bly: th e files th a t m a ke it u p,
th e pu blicly expor ted types im plem en ted by th e files in th e a ssem bly, a n d th e r esou r ce or da ta files th a t a r e a ssocia ted
with it.
By defa u lt, com piler s a ctu a lly do th e wor k of tu r n in g th e em itted m a n a ged m odu le in to a n a ssem bly. Th a t is, th e C#
com piler em its a m a n a ged m odu le th a t con ta in s a m a n ifest. Th e m a n ifest in dica tes th a t th e a ssem bly con sists of ju st th e
Architecture of the .NET Framework
5
on e file. So for pr ojects th a t h a ve ju st on e m a n a ged m odu le a n d n o r esou r ce files, th e a ssem bly will be th e m a n a ged
m odu le, a n d you don t h a ve a n y a ddition a l steps to per for m du r in g you r bu ild pr ocess. I f you wish to gr ou p a set of files
in to a n a ssem bly, th en you will h a ve to be a wa r e of m or e tools ( su ch a s th e a ssem bly lin ker , AL.exe) a n d th eir
com m a n d- lin e option s. Th ese tools a n d option s a r e expla in ed in th e n ext ch a pter .
An a ssem bly a llows you to decou ple th e logica l a n d ph ysica l n otion s of a r eu sa ble, deploya ble, ver sion a ble com pon en t.
Th e wa y in wh ich you pa r tition you r code a n d r esou r ces in to differ en t files is com pletely u p to you . For exa m ple, you
cou ld pu t r a r ely u sed types or r esou r ces in sepa r a te files th a t a r e pa r t of a n a ssem bly. Th e sepa r a te files cou ld be
down loa ded fr om th e web a s n eeded. I f th e files a r e n ever n eeded, th ey' r e n ever down loa ded, sa vin g disk spa ce a n d
r edu cin g in sta lla tion tim e. An a ssem bly lets you br ea k u p th e ph ysica l deploym en t of th e files bu t still tr ea t th em a s a
sin gle collection .
Th e m odu les in a n a ssem bly a lso in clu de in for m a tion , in clu din g ver sion n u m ber s, a bou t r efer en ced a ssem blies. Th is
in for m a tion m a kes a n a ssem bly self-describing. I n oth er wor ds, th e CLR kn ows ever yth in g th a t a n a ssem bly n eeds in
or der to execu te. No a ddition a l in for m a tion is r equ ir ed in th e r egistr y or in th e Active Dir ector y, so deployin g a ssem blies
is m u ch ea sier th a n deployin g u n m a n a ged com pon en ts.
Loading the Common Language Runtime
Ea ch a ssem bly th a t you bu ild ca n eith er be a n execu ta ble a pplica tion or a DLL con ta in in g a set of types ( com pon en ts)
for u se by a n execu ta ble a pplica tion . Of cou r se, th e CLR is r espon sible for m a n a gin g th e execu tion of code con ta in ed
with in th ese a ssem blies. Th is m ea n s th a t th e .NET Fr a m ewor k m u st be in sta lled on th e h ost m a ch in e. Micr osoft h a s
cr ea ted a r edistr ibu tion pa cka ge th a t you ca n fr eely sh ip to in sta ll th e .NET Fr a m ewor k on you r cu stom er ' s m a ch in es.
Fu tu r e ver sion s of Win dows will in clu de th e .NET Fr a m ewor k, a t wh ich poin t you will n o lon ger n eed to sh ip it with you r
a ssem blies.
You ca n tell wh eth er th e .NET Fr a m ewor k h a s been in sta lled by lookin g for th e MSCor EE.dll file in th e
%win dir %\ system 32 dir ector y. Th e existen ce of th is file tells you th a t th e .NET Fr a m ewor k is in sta lled. However , sever a l
ver sion s of th e .NET Fr a m ewor k m a y be in sta lled on a sin gle m a ch in e sim u lta n eou sly. I f you wa n t to deter m in e exa ctly
wh ich ver sion s of th e .NET Fr a m ewor k a r e in sta lled, exa m in e th e su bkeys u n der th e followin g r egistr y key:
HKEY_ LOCAL_ MACHI NE\ SOFTWARE\ Micr osoft\ .NETFr a m ewor k\ policy.
Wh en you bu ild a n EXE a ssem bly, th e com piler / lin ker em its som e specia l in for m a tion in to th e r esu ltin g a ssem bly' s PE
File h ea der a n d th e file' s .text section . Wh en th e EXE file is in voked, th is specia l in for m a tion ca u ses th e CLR to loa d a n d
in itia lize. Th en th e CLR loca tes th e en tr y poin t m eth od for th e a pplica tion a n d lets th e a pplica tion sta r t execu tin g.
Sim ila r ly, if a n u n m a n a ged a pplica tion ca lls LoadLibrary to loa d a m a n a ged a ssem bly, th e en tr y poin t
fu n ction for th e DLL kn ows to loa d th e CLR in or der to pr ocess th e code con ta in ed with in th e a ssem bly.
For th e m ost pa r t, you do n ot n eed to kn ow a bou t or u n der sta n d h ow th e CLR a ctu a lly gets loa ded. For th e cu r iou s,
h owever , I will expla in h ow a m a n a ged EXE or DLL sta r ts th e CLR. I f you r e n ot in ter ested in th is, feel fr ee to skip to th e
n ext section .
Th e figu re on th e n ext pa ge su m m a r izes h ow a m a n a ged EXE loa ds a n d in itia lizes th e CLR.
Applied .NET Framework Programming
6

Wh en th e com piler / lin ker cr ea tes a n execu ta ble a ssem bly, th e followin g 6- byte x86 stu b fu n ction is em itted in to th e .text
section of th e PE file:
JMP _CorExeMain
Th e _CorExeMain fu n ction is im por ted fr om th e Micr osoft MSCor EE.dll dyn a m ic- lin k libr a r y, a n d th er efor e
MSCor EE.dll is r efer en ced in th e im por t ( .ida ta ) section of th e a ssem bly file. ( MSCor EE.dll sta n ds for Micr osoft
Com pon en t Object Ru n tim e Execu tion En gin e.) Wh en th e m a n a ged EXE file is in voked, Win dows tr ea ts it ju st like a n y
n or m a l ( u n m a n a ged) EXE file: th e Win dows loa der loa ds th e file a n d exa m in es th e .ida ta section to see th a t
MSCor EE.dll sh ou ld be loa ded in to th e pr ocesss a ddr ess spa ce. Th en , th e loa der obta in s th e a ddr ess of th e
_CorExeMain fu n ction in side MSCor EE.dll a n d fixes u p th e stu b fu n ction s JMP in str u ction in th e m a n a ged
EXE file.
Th e pr im a r y th r ea d for th e pr ocess begin s execu tin g th is x86 stu b fu n ction , wh ich im m edia tely ju m ps to
_CorExeMain in MSCor EE.dll. _CorExeMain in itia lizes th e CLR a n d th en looks a t th e CLR h ea der for th e
execu ta ble a ssem bly to deter m in e wh a t m a n a ged en tr y poin t m eth od sh ou ld execu te. Th e I L code for th e m eth od is th en
com piled in to n a tive CPU in str u ction s, a fter wh ich th e CLR ju m ps to th e n a tive code ( u sin g th e pr ocesss pr im a r y
th r ea d) . At th is poin t, th e m a n a ged a pplica tion code is r u n n in g.
Th e situ a tion is sim ila r for a m a n a ged DLL. Wh en bu ildin g a m a n a ged DLL, th e com piler / lin ker em its a sim ila r 6- byte
x86 stu b fu n ction for a DLL a ssem bly in th e .text section of th e PE file:
JMP _CorDllMain
Architecture of the .NET Framework
7
Th e _CorDllMain fu n ction is a lso im por ted fr om th e MSCor EE.dll, ca u sin g th e .ida ta section for th e DLL to
r efer en ce MSCor EE.dll. Wh en Win dows loa ds th e DLL, it a u tom a tica lly loa ds MSCor EE.dll ( if it isn t a lr ea dy loa ded) ,
obta in s th e a ddr ess of th e _CorDllMain fu n ction , a n d fixes u p th e 6 byte x86 JMP stu b in th e m a n a ged DLL.
Th e th r ea d th a t ca lled LoadLibrary to loa d th e m a n a ged DLL th en ju m ps to th e x86 stu b in th e m a n a ged DLL
a ssem bly, wh ich im m edia tely ju m ps to th e _CorDllMain in MSCor EE.dll. _CorDllMain in itia lizes th e
CLR ( if it h a sn t a lr ea dy been in itia lized for th e pr ocess) a n d th en r etu r n s so th a t th e a pplica tion ca n con tin u e execu tin g
a s n or m a l.
Th ese 6- byte x86 stu b fu n ction s a r e r equ ir ed to r u n m a n a ged a ssem blies on Win dows 98, Win dows 98SE, Win dows ME,
Win dows NT 4, a n d Win dows 2000 beca u se a ll th ese oper a tin g system s sh ipped lon g befor e th e CLR beca m e a va ila ble.
Note th a t th e 6- byte stu b fu n ction is specifica lly for x86 m a ch in es. Th is stu b does n ot wor k pr oper ly if th e CLR is por ted
to r u n on oth er CPU a r ch itectu r es. Beca u se Win dows XP a n d th e Win dows .NET Ser ver s su ppor t both th e x86 a n d th e
I A64 CPU a r ch itectu r es, th e loa der for Win dows XP a n d th e Win dows .NET Ser ver s wa s m odified to look specifica lly for
m a n a ged a ssem blies.
On Win dows XP a n d th e Win dows .NET Ser ver s, wh en a m a n a ged a ssem bly is in voked ( typica lly via
CreateProcess or LoadLibrary) , th e OS loa der detects th a t th e file con ta in s m a n a ged code. I t does th is
by exa m in in g dir ector y en tr y 14 in th e PE file h ea der ( see
IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR in Win NT.h ) . I f th is dir ector y en tr y exists a n d is n ot
0, th en th e loa der ign or es th e files im por t ( .ida ta ) section en tir ely a n d kn ows to a u tom a tica lly loa d MSCor EE.dll in to
th e a ddr ess spa ce for th e pr ocess. On ce loa ded, th e OS loa der m a kes th e pr ocess th r ea d ju m p dir ectly to th e cor r ect
fu n ction in MSCor EE.dll. Th e 6- byte x86 stu b fu n ction s a r e ign or ed on m a ch in es r u n n in g Win dows XP a n d th e Win dows
.NET Ser ver s.
On e la st n ote a bou t m a n a ged PE files: Ma n a ged PE files a lwa ys u se th e 32- bit PE file for m a t; th ey do n ot u se th e n ewer
64- bit PE file for m a t. On 64- bit Win dows system s, th e OS loa der detects th e m a n a ged 32- bit PE file a n d a u tom a tica lly
kn ows to cr ea te a 64- bit a ddr ess spa ce.
Executing the Code in Your Assembly
As m en tion ed ea r lier , m a n a ged m odu les con ta in both m eta da ta a n d in ter m edia te la n gu a ge ( I L) code. I L is a CPU-
in depen den t m a ch in e la n gu a ge cr ea ted by Micr osoft a fter con su lta tion with sever a l exter n a l com m er cia l a n d a ca dem ic
la n gu a ge/ com piler wr iter s. I L is m u ch h igh er - level th a n m ost CPU m a ch in e la n gu a ges. I L u n der sta n ds object types a n d
h a s in str u ction s th a t cr ea te a n d in itia lize objects, ca ll vir tu a l m eth ods on objects, a n d m a n ipu la te a r r a y elem en ts
dir ectly. I t even h a s in str u ction s th a t th r ow a n d ca tch exception s for er r or h a n dlin g. You ca n th in k of I L a s a n object-
or ien ted m a ch in e la n gu a ge.
Usu a lly, developer s pr efer to pr ogr a m in a h igh - level la n gu a ge, su ch a s C# or Visu a l Ba sic.NET. Th e com piler s for a ll
th ese h igh - level la n gu a ges pr odu ce I L. However , like a n y oth er m a ch in e la n gu a ge, I L ca n be wr itten in a ssem bly
la n gu a ge a n d Micr osoft does pr ovide a n I L a ssem bler , I LAsm .exe. Micr osoft a lso pr ovides a n I L disa ssem bler ,
I LDa sm .exe.
Some people are concerned that IL does not offer enough intellectual property protection for their
algorithms. In other words, you could build a managed module and someone else could use a tool, like
an IL disassembler, to reverse engineer exactly what your application code does.
Yes, its true that IL code is higher-level than most other assembly languages and, in general, reverse
engineering IL code is relatively simple. However, when you implement a web service or web form
Applied .NET Framework Programming
8
application, your managed module resides on your server, inaccessible to anyone outside your
company. Outsiders cannot use any tool to see the IL code, so your intellectual property is completely
safe.
If you are concerned about any of your managed modules that you do distribute, then you can use the
Microsoft obfuscator utility (OB.exe), which is downloadable from http://www.GotDotNet.com. This
utility scrambles the names of all the private symbols in your managed module metadata. It will be
difficult for someone to unscramble the names and understand the purpose of each method. Note that
the Microsoft obfuscator scrambles metadata names only and does not scramble the IL code in anyway
since the CLR must be able to process unscrambled IL.
If you dont feel that the obfuscator offers the kind of intellectual property protection that you desire,
then you can consider implementing your more-sensitive algorithms in some unmanaged module, which
will contain native CPU instructions instead of IL and metadata. Then, you can use the CLRs
interoperability features to communicate between the managed and unmanaged portions of your
application. Of course, this assumes that youre not worried about people reverse engineering the native
CPU instructions in your unmanaged code.
An y h igh - level la n gu a ge will m ost likely expose on ly a su bset of th e fa cilities offer ed by th e CLR. I L a ssem bly la n gu a ge,
h owever , gives a developer a ccess to a ll th e fa cilities of th e CLR. So if you r pr ogr a m m in g la n gu a ge of ch oice h ides a CLR
fea tu r e th a t you r ea lly wish to ta ke a dva n ta ge of, you ca n wr ite th a t por tion of you r code in I L a ssem bly or in a n oth er
pr ogr a m m in g la n gu a ge th a t exposes th e CLR fea tu r e you seek.
Th e on ly wa y for you to kn ow wh a t fa cilities a r e offer ed by th e r u n tim e is to r ea d docu m en ta tion specific to th e CLR
itself. I n th is book, I tr y to con cen tr a te on CLR fea tu r es a n d h ow th ey a r e exposed or n ot exposed by th e C# la n gu a ge. I
su spect th a t m ost oth er books a n d a r ticles will pr esen t th e CLR via a pa r ticu la r la n gu a ge a n d th a t m ost developer s will
com e to believe th a t th e CLR offer s on ly th ose fea tu r es th a t th e developer ' s ch osen la n gu a ge exposes. As lon g a s you r
la n gu a ge a llows you to a ccom plish wh a t you ' r e tr yin g to do, th is blu r r ed per spective is n ot a ba d th in g.
Personally, I feel that an ability to switch programming languages easily with rich integration between
languages is an awesome feature of the CLR. I also believe that this is a feature that will, unfortunately,
often be overlooked by developers.
Programming languages like C# and Visual Basic.NET are excellent languages for doing I/O operations.
APL is an awesome language for doing advanced engineering or financial calculations. Through the CLR,
you can write the I/O portions of your application using C# and then write the engineering calculations
using APL. The CLR offers a level of integration between these languages that is unprecedented, and
really makes mixed-language programming worthy of consideration for many development projects.
An im por ta n t th in g to n ote a bou t I L is th a t it is n ot tied to a n y specific CPU pla tfor m . Th is m ea n s th a t a m a n a ged
m odu le con ta in in g I L ca n r u n on a n y CPU pla tfor m a s lon g a s th e oper a tin g system r u n n in g on th a t CPU pla tfor m
h osts a ver sion of th e CLR. Alth ou gh th e in itia l r elea se of th e CLR will r u n on ly on 32- bit Win dows pla tfor m s, developin g
a n a pplica tion u sin g m a n a ged I L sets u p a developer to be m or e in depen den t of th e u n der lyin g CPU a r ch itectu r e.
In October 2000, Microsoft (along with Intel and Hewlett-Packard as co-sponsors) proposed a large
subset of the .NET Frameworks to the ECMA (the European Computer Manufacturers Association) for
the purpose of standardization. The ECMA accepted this proposal and created a technical committee
(TC39) to oversee the standardization process. The technical committee is charged with the following
duties:
Architecture of the .NET Framework
9
Tech n ica l Gr ou p 1: Develop a dyn a m ic scr iptin g la n gu a ge sta n da r d ( ECMAScr ipt) . Micr osofts
im plem en ta tion of ECMAScr ipt is JScr ipt.
Tech n ica l Gr ou p 2: Develop a sta n da r dized ver sion of th e C# pr ogr a m m in g la n gu a ge.
Tech n ica l Gr ou p 3: Develop a com m on la n gu a ge in fr a str u ctu r e ( CLI ) ba sed on a su bset of th e
fu n ction a lity offer ed by th e .NET Fr a m ewor ks CLR a n d cla ss libr a r y. Specifica lly, th e CLI defin es a file
for m a t, a com m on type system , a n exten sible m eta da ta system , a n in ter m edia te la n gu a ge ( I L) , a n d
a ccess to th e u n der lyin g pla tfor m ( P/ I n voke) . I n a ddition , th e CLI a lso defin es a fa ctor a ble ( to a llow for
sm a ll h a r dwa r e devices) ba se cla ss libr a r y design ed for u se by m u ltiple pr ogr a m m in g la n gu a ges.
Once the standardization is completed, these standards will be contributed to ISO/IEC JTC 1
(Information Technology). At this time, the technical committee will investigate further directions for
CLI, C#, and ECMAScript, as well as entertain proposals for any complementary or additional
technology. For more information about ECMA, please see http://www.ECMA.ch and
http://MSDN.Microsoft.com/Net/ECMA.
With the standardization of the CLI, C#, and ECMAScript, Microsoft wont own any of these
technologies. Microsoft will simply be one company of many (hopefully) that are producing
implementations of these technologies. Certainly Microsoft hopes that their implementation will be the
best in terms of performance and customer-demand-driven features. This is what will help sales of
Windows, since the Microsoft best of breed implementation will only run on Windows. However, other
companies are free to implement these standards and compete against Microsoft.
Of cou r se, I L in str u ction s ca n n ot be execu ted directlyby toda ys CPUs ( a lth ou gh th is m a y ch a n ge som eda y) . I n or der to
execu te a m eth od, its I L code m u st fir st be con ver ted to n a tive CPU in str u ction s. To m a ke th is con ver sion , th e CLR
pr ovides a JI T ( ju st- in - tim e) com piler .
Th e figu r e below sh ows wh a t h a ppen s th e fir st tim e a m eth od is ca lled.
Applied .NET Framework Programming
10

Ju st befor e th e Main m eth od execu tes, th e CLR detects a ll th e types th a t a r e r efer en ced by th e code in Main. Th is
ca u ses th e CLR to a lloca te a n in ter n a l da ta str u ctu r e th a t is u sed to m a n a ge a ccess to ea ch r efer en ced type. I n th e figu r e
a bove, th e Main m eth od r efer s to a sin gle type, Console, ca u sin g th e CLR to a lloca te a sin gle in ter n a l str u ctu r e.
Th is in ter n a l da ta str u ctu r e con ta in s a n en tr y for ea ch m eth od defin ed by th e type. Ea ch en tr y h olds th e a ddr ess wh er e
th e m eth od im plem en ta tion ca n be fou n d. Wh en in itia lizin g th is str u ctu r e, th e CLR sets ea ch en tr y to a fu n ction
con ta in ed in side th e CLR itself. I ca ll th is fu n ction JITCompiler.
Wh en Main m a kes its fir st ca ll to WriteLine, th e JITCompiler fu n ction is ca lled. Th e
JITCompiler fu n ction is r espon sible for com pilin g a m eth ods I L code in to n a tive CPU in str u ction s. Sin ce th e I L
is bein g com piled ju st in tim e , th is com pon en t of th e CLR is fr equ en tly r efer r ed to a s a JITter or JIT Compiler.
Wh en ca lled, th e JITCompiler fu n ction kn ows wh a t m eth od is bein g ca lled a n d wh a t type defin es th is m eth od.
Th e JITCompiler fu n ction th en sea r ch es th e defin in g a ssem blys m eta da ta for th e ca lled m eth ods I L.
JITCompiler ver ifies a n d com piles th e I L code in to n a tive CPU in str u ction s, wh ich a r e th en sa ved in a
dyn a m ica lly a lloca ted block of m em or y. JITCompiler goes ba ck to th e types in ter n a l da ta str u ctu r e a n d r epla ces
th e a ddr ess of th e ca lled m eth od with th e a ddr ess of th e block of m em or y con ta in in g th e n a tive CPU in str u ction s.
Fin a lly, JITCompiler ju m ps to th e code in th e m em or y block. Th is code is th e im plem en ta tion of th e
WriteLine m eth od ( th e ver sion th a t ta kes a String pa r a m eter ) . Wh en th is code r etu r n s, execu tion r esu m es in
Main, a s n or m a l.
Su ppose n ow th a t Main ca lls WriteLine a secon d tim e. Th is tim e, beca u se th e code for WriteLine h a s
a lr ea dy been ver ified a n d com piled, th e ca ll goes dir ectly to th e n a tive code in th e m em or y block, skippin g th e
JITCompiler fu n ction en tir ely. After th e WriteLine m eth od execu tes it r etu r n s to Main. Th e figu r e below
sh ows wh a t th e situ a tion looks like wh en WriteLine is ca lled th e secon d tim e:
Architecture of the .NET Framework
11

Th e im por ta n t th in g to n ote is th a t th e pr ocess in cu r s a per for m a n ce h it on ly th e fir st tim e a m eth od is ca lled. All
su bsequ en t ca lls to th e m eth od execu te a t th e fu ll speed of th e n a tive code: Ver ifica tion a n d com pila tion to n a tive code is
n ot per for m ed a ga in .
Note th a t th e JI T com piler stor es th e n a tive CPU in str u ction s in dyn a m ic m em or y: Th e com piled code is disca r ded wh en
th e a pplica tion ter m in a tes. So if you r u n th e a pplica tion in th e fu tu r e or if you r u n two in sta n ces of th e a pplica tion
sim u lta n eou sly ( in two differ en t oper a tin g system pr ocesses) , th en th e JI T com piler will h a ve to com pile th e I L to n a tive
in str u ction s a ga in .
For m ost a pplica tion s, th e per for m a n ce h it in cu r r ed by JI T com pila tion is n ot sign ifica n t. Most a pplica tion s ten d to ca ll
th e sa m e m eth ods over a n d over a ga in , so th ese m eth ods will ta ke th e per for m a n ce h it on ly on ce wh ile th e a pplica tion
execu tes. Also, a pr ocess n or m a lly spen ds m or e tim e in side th e m eth od th a n it spen ds ca llin g th e m eth od.
Be a wa r e th a t th e JI T com piler optim izes th e n a tive code ju st like th e ba ck en d of a n u n m a n a ged C+ + com piler . Aga in ,
th e optim iza tion m a y ta ke tim e, bu t th e code will execu te with m u ch better per for m a n ce th a n u n optim ized code.
Developer s com in g fr om a n u n m a n a ged C or C+ + ba ckgr ou n d a r e pr oba bly th in kin g a bou t th e per for m a n ce
r a m ifica tion s of a ll th is. After a ll, u n m a n a ged code is com piled for a specific CPU pla tfor m , a n d wh en in voked, th e code
ca n sim ply execu te. I n th is m a n a ged en vir on m en t, com pilin g th e code is a ccom plish ed in two ph a ses. Fir st, th e com piler
pa sses over ou r sou r ce code, doin g a s m u ch wor k a s possible in pr odu cin g I L. Bu t th en , in or der to a ctu a lly execu te th e
code, th e I L itself m u st be com piled in to n a tive CPU in str u ction s a t r u n tim e, r equ ir in g th a t m or e m em or y be a lloca ted,
a n d r equ ir in g a ddition CPU tim e to do th e wor k.
Believe m e, I a ppr oa ch ed th e CLR fr om a C/ C+ + ba ckgr ou n d m yself, so I wa s qu ite skeptica l a n d con cer n ed a bou t th is
a ddition a l over h ea d. Th e tr u th is th a t th is secon d com pila tion sta ge th a t occu r s a t r u n tim e does h u r t per for m a n ce a n d it
does a lloca te dyn a m ic m em or y. However , Micr osoft h a s don e a lot of per for m a n ce wor k to keep th is a ddition a l over h ea d
to a m in im u m .
Applied .NET Framework Programming
12
I f you too a r e skeptica l, th en you sh ou ld cer ta in ly bu ild som e a pplica tion s to test th e per for m a n ce for you r self. I n
a ddition , you sh ou ld r u n som e n on - tr ivia l m a n a ged a pplica tion s pr odu ced by Micr osoft or oth er s a n d m ea su r e th eir
per for m a n ce. I th in k you ' ll be su r pr ised a t h ow good th e per for m a n ce a ctu a lly is.
I n fa ct, h a r d a s th is m igh t be to believe, m a n y people ( in clu din g m e) th in k th a t m a n a ged a pplica tion s cou ld a ctu a lly
ou t- per for m u n m a n a ged a pplica tion s. For exa m ple, wh en th e JI T com piler com piles th e I L code in to n a tive code a t
r u n tim e, th e com piler kn ows m or e a bou t th e execu tion en vir on m en t th a n a n u n m a n a ged com piler wou ld kn ow. Her e
a r e som e wa ys th a t m a n a ged code cou ld ou t- per for m u n m a n a ged code:
A JI T com piler cou ld detect th a t th e a pplica tion is r u n n in g on a Pen tiu m 4 a n d pr odu ce n a tive code th a t
ta kes a dva n ta ge of a n y specia l in str u ction s offer ed by th e Pen tiu m 4. Usu a lly, u n m a n a ged a pplica tion s
a r e com piled for th e lowest com m on den om in a tor CPU a n d a void u sin g specia l in str u ction s th a t wou ld
give th e a pplica tion a per for m a n ce boost on n ewer CPUs.
A JI T com piler cou ld detect th a t a cer ta in test is a lwa ys fa lse on th e cu r r en t h ost m a ch in e. For exa m ple, a
m eth od with code like th is:
if (numberOfCPUs > 1) {
...
}
cou ld ca u se th e JI T com piler to n ot gen er a te a n y CPU in str u ction s for th e a bove code if th e h ost m a ch in e
h a s on ly 1 CPU. I n th is ca se, th e n a tive code h a s been fin e- tu n ed for th e h ost m a ch in e: th e code is
sm a ller a n d execu tes fa ster .
Th e CLR cou ld pr ofile th e code execu tion a n d r ecom pile th e I L in to n a tive code wh ile th e a pplica tion
r u n s. Th e r ecom piled code cou ld be r eor ga n ized to r edu ce in cor r ect br a n ch pr ediction s depen din g on th e
obser ved execu tion pa tter n s.
For th ese a n d m a n y oth er r ea son s, you sh ou ld expect fu tu r e a ccom plish m en ts with m a n a ged code to execu te better th a n
toda y' s u n m a n a ged code. As I sa id, th e per for m a n ce toda y is qu ite good for m ost a pplica tion s, a n d it pr om ises to
im pr ove a s tim e goes on .
I f you r exper im en ts sh ow th a t th e CLR' s JI T com piler does n ot offer you r a pplica tion th e kin d of per for m a n ce it r equ ir es,
th en you m igh t wa n t to ta ke a dva n ta ge of th e NGen .exe tool th a t sh ips with th e .NET Fr a m ewor k SDK. Th is tool
com piles a ll th e I L code for a n a ssem bly in to n a tive code a n d sa ves th e r esu ltin g n a tive code to a file on disk. At r u n tim e,
wh en a n a ssem bly is loa ded, th e CLR a u tom a tica lly ch ecks to see wh eth er a pr ecom piled ver sion of th e a ssem bly a lso
exists, a n d if it does, th e CLR loa ds th e pr ecom piled code so th a t n o com pila tion a t r u n tim e is r equ ir ed.
IL and Verification
I L is sta ck- ba sed, wh ich m ea n s th a t a ll its in str u ction s pu sh oper a n ds on to a n execu tion sta ck a n d pop r esu lts off th e
sta ck. Accor din gly, I L offer s n o in str u ction s to m a n ipu la te r egister s. Com piler developer s ca n ea sily pr odu ce I L code:
Th ey don ' t h a ve to th in k a bou t m a n a gin g r egister s, a n d th er e a r e fewer I L in str u ction s ( sin ce n on e exist for
m a n ipu la tin g r egister s) .
An d h er es a n oth er sim plifica tion : I L in str u ction s a r e typeless. For exa m ple, I L offer s a n a dd in str u ction th a t a dds th e la st
two oper a n ds pu sh ed on th e sta ck. I L does n ot offer a 32- bit a dd in str u ction a n d a 64- bit a dd in str u ction . Wh en th e a dd
in str u ction execu tes, it deter m in es th e types of th e oper a n ds on th e sta ck a n d per for m s th e a ppr opr ia te oper a tion .
Architecture of the .NET Framework
13
I n m y opin ion , th e biggest ben efit of I L is n ot th a t it a bstr a cts a wa y th e u n der lyin g CPU. Th e biggest ben efit is
a pplica tion r obu stn ess. Wh ile com pilin g I L in to n a tive CPU in str u ction s, th e CLR per for m s a pr ocess ca lled verification.
Ver ifica tion exa m in es th e h igh - level I L code a n d en su r es th a t ever yth in g it does is sa fe. For exa m ple, ver ifica tion
ch ecks th a t n o m em or y is r ea d fr om with ou t h a vin g pr eviou sly been wr itten to, th a t ever y m eth od is ca lled with th e
cor r ect n u m ber of pa r a m eter s, th a t ea ch pa r a m eter is of th e cor r ect type, th a t ever y m eth ods r etu r n va lu e is u sed
pr oper ly, th a t ever y m eth od h a s a r etu r n sta tem en t, a n d so on .
Th e m eta da ta for ea ch m a n a ged m odu le in clu des a ll th e m eth od a n d type in for m a tion u sed by th e ver ifica tion pr ocess.
I f th e I L code is deter m in ed to be u n sa fe, th en a System.Security.VerifierException
exception is th r own , pr even tin g th e m eth od fr om execu tin g.
By default, Microsofts C# and Visual Basic.NET compilers produce safe code. Safe code is code that is
verifiably safe. However, using C#s unsafe keyword or using other languages (such as C++ with
Managed Extensions or IL Assembly language), you can produce code that cannot be verifiably safe.
That is, the code might, in fact, be safe, but the verification is unable to prove it.
To ensure that all methods in your managed module contain verifiably safe IL, you can use the
PEVerify.exe utility that ships with the .NET Framework SDK. When Microsoft developers test their C#
and Visual Basic.NET compilers, they run the resulting module through PEVerify to ensure that the
compiler always produces verifiably safe code. If PEVerify detects unsafe code, then Microsoft fixes the
compiler.
You may want to consider running PEVerify on your own modules before you package and ship them. If
PEVerify detects a problem, then there is a bug in the compiler and you should report this to Microsoft
(or whatever company produces the compiler you're using). If PEVerify doesn't detect any unverifiable
code, then you know that your code will run without throwing a VerifierException on the end-
user's machine.
Note that an administrator can elect to turn verification off (using the ".NET Management" Microsoft
Management Console Snap-In). With verification off, the JIT compiler will compile unverifiable IL into
native CPU instructions; however, the administrator is taking full responsibility for the code's behavior.
I n Win dows, ea ch pr ocess h a s its own vir tu a l a ddr ess spa ce. Sepa r a te a ddr ess spa ces a r e n ecessa r y beca u se Win dows ca n t
tr u st th e a pplica tion code. I t is en tir ely possible ( a n d u n for tu n a tely, a ll too com m on ) th a t a n a pplica tion will r ea d fr om
or wr ite to a n in va lid m em or y a ddr ess. Pla cin g ea ch Win dows pr ocess in a sepa r a te a ddr ess spa ce en h a n ces r obu stn ess:
On e pr ocess ca n n ot a dver sely a ffect a n oth er pr ocess.
However , by ver ifyin g th e m a n a ged code, we kn ow th a t th e code does n ot im pr oper ly a ccess m em or y a n d ca n n ot
a dver sely a ffect a n oth er a pplica tion s code. Th is m ea n s th a t we ca n r u n m u ltiple m a n a ged a pplica tion s in a sin gle
Win dows vir tu a l a ddr ess spa ce.
Beca u se Win dows pr ocesses r equ ir e a lot of oper a tin g system r esou r ces, la u n ch in g m a n y pr ocesses ca n h u r t per for m a n ce
a n d lim it a va ila ble OS r esou r ces. Ru n n in g m u ltiple a pplica tion s in a sin gle OS pr ocess r edu ces th e n u m ber of pr ocesses,
wh ich ca n im pr ove per for m a n ce, r equ ir e fewer r esou r ces, a n d offer equ iva len t r obu stn ess. Th is is a n oth er ben efit of
m a n a ged code a s com pa r ed to u n m a n a ged code.
Th e CLR does, in fa ct, offer th e a bility to execu te m u ltiple m a n a ged a pplica tion s in a sin gle OS pr ocess. Ea ch m a n a ged
a pplica tion is ca lled a n AppDomain. By defa u lt, ever y m a n a ged EXE will r u n in its own sepa r a te a ddr ess spa ce th a t h a s
Applied .NET Framework Programming
14
ju st th e on e AppDom a in . However , a pr ocess h ostin g th e CLR ( su ch a s I I S or a fu tu r e ver sion of SQL Ser ver ) ca n decide
to r u n AppDom a in s in a sin gle OS pr ocess.
The .NET Framework Class Library
I n clu ded with th e .NET Fr a m ewor k is a set of Fr a m ewor k Cla ss Libr a r y ( FCL) a ssem blies th a t con ta in s sever a l th ou sa n d
type defin ition s, wh er e ea ch type exposes som e fu n ction a lity. All in a ll, th e CLR a n d th e FCL a llow developer s to bu ild th e
followin g kin ds of a pplica tion s:
Web Ser vices. Com pon en ts th a t ca n be a ccessed over th e I n ter n et ver y ea sily. Web ser vices a r e, of cou r se,
th e m a in th r u st of Micr osoft' s .NET in itia tive.
Web For m s. HTML- ba sed a pplica tion s ( web sites) . Typica lly, web for m a pplica tion s m a ke da ta ba se
qu er ies a n d web ser vice ca lls, com bin e a n d filter th e r etu r n ed in for m a tion , a n d th en pr esen t th a t
in for m a tion in a br owser u sin g a r ich HTML- ba sed UI . Web for m s pr ovide a Visu a l Ba sic 6- a n d I n ter Dev-
like developm en t en vir on m en t for web a pplica tion s wr itten in a n y CLR la n gu a ge.
Win dows For m s. Rich Win dows GUI a pplica tion s. I n stea d of u sin g a web for m to cr ea te you r
a pplica tion ' s UI , you ca n u se th e m or e power fu l, h igh er - per for m a n ce fu n ction a lity offer ed by th e
Win dows desktop. Win dows for m a pplica tion s ca n ta ke a dva n ta ge of con tr ols, m en u s, m ou se a n d
keyboa r d even ts, a n d ca n ta lk dir ectly to th e u n der lyin g oper a tin g system . Like web for m a pplica tion s,
Win dows for m a pplica tion s m a ke da ta ba se qu er ies a n d ca ll web ser vices. Win dows For m s pr ovides a
Visu a l Ba sic 6- like developm en t en vir on m en t for GUI a pplica tion s wr itten in a n y CLR la n gu a ge.
Win dows Con sole Applica tion s. For a pplica tion s with ver y sim ple UI dem a n ds, a con sole a pplica tion
pr ovides a qu ick a n d ea sy solu tion . Com piler s, u tilities, a n d tools a r e typica lly im plem en ted a s con sole
a pplica tion s.
Win dows Ser vices. Yes, it is possible to bu ild ser vice a pplica tion s con tr olla ble via th e Win dows Ser vice
Con tr ol Ma n a ger ( SCM) u sin g th e .NET Fr a m ewor k.
Com pon en t Libr a r y. Of cou r se, th e .NET Fr a m ewor k a llows you to bu ild sta n d- a lon e com pon en ts
( types) th a t m a y be ea sily in cor por a ted in to a n y of th e a bove m en tion ed a pplica tion types.
Sin ce th e FCL con ta in s liter a lly th ou sa n ds of types, a set of r ela ted types is pr esen ted to th e developer with in a sin gle
namespace. For exa m ple, th e System n a m espa ce ( wh ich you sh ou ld becom e m ost fa m ilia r with ) con ta in s th e Object
ba se type, fr om wh ich a ll oth er types u ltim a tely der ive. I n a ddition , th e System n a m espa ce con ta in s types for in teger s,
ch a r a cter s, str in gs, exception h a n dlin g, a n d con sole I / O, a s well a s a bu n ch of u tility types th a t con ver t sa fely between
da ta types, for m a t da ta types, gen er a te r a n dom n u m ber s, a n d per for m va r iou s m a th fu n ction s. All a pplica tion s u se types
fr om th e System n a m espa ce.
To a ccess a n y pla tfor m fea tu r e, you n eed to kn ow wh ich n a m espa ce con ta in s th e types th a t expose th e fa cility you ' r e
a fter . I f you wa n t to cu stom ize th e beh a vior of a n y type, you ca n sim ply der ive you r own type fr om th e desir ed FCL type.
Th e .NET Fr a m ewor k r elies on th e object- or ien ted n a tu r e of th e pla tfor m to pr esen t a con sisten t pr ogr a m m in g pa r a digm
to softwa r e developer s. I t a lso en a bles developer s to cr ea te th eir own n a m espa ces con ta in in g th eir own types, wh ich
m er ge sea m lessly in to th e pr ogr a m m in g pa r a digm . Com pa r ed to Win 32 pr ogr a m m in g pa r a digm s, th is gr ea tly sim plifies
softwa r e developm en t.
Most of th e n a m espa ces in th e FCL pr esen t types th a t you ca n u se for a n y kin d of a pplica tion . Th e ta ble below lists som e
of th e m or e gen er a l n a m espa ces, with a br ief descr iption of wh a t th e types in th a t n a m espa ce a r e u sed for :
Architecture of the .NET Framework
15
Namespace Purpose of Types
System All th e ba sic types u sed by ever y a pplica tion .
System .Collection s Ma n a gin g collection s of objects. I n clu des th e popu la r collection types su ch a s
Sta cks, Qu eu es, Ha sh ta bles, a n d so on .
System .Dia gn ostics I n str u m en tin g a n d debu ggin g you r a pplica tion .
System .Dr a win g Ma n ipu la tin g 2D gr a ph ics. Typica lly u sed for Win dows For m s a pplica tion s a n d
for cr ea tin g im a ges th a t a r e to a ppea r in a web for m .
System .En ter pr iseSer vices Ma n a gin g tr a n sa ction s, qu eu ed com pon en ts, object poolin g, ju st- in - tim e
a ctiva tion , secu r ity, a n d oth er fea tu r es to m a ke th e u se of m a n a ged code m or e
efficien t on th e ser ver .
System .Globa liza tion Na tion a l La n gu a ge Su ppor t ( NLS) , su ch a s str in g com pa r es, for m a ttin g,
a n d ca len da r s.
System .I O Doin g str ea m I / O, wa lkin g dir ector ies a n d files.
System .Ma n a gem en t Ma n a gin g oth er com pu ter s in th e en ter pr ise via WMI .
System .Net Networ k com m u n ica tion s.
System .Reflection I n spectin g m eta da ta a n d la te bin din g to types a n d th eir m em ber s.
System .Resou r ces Ma n ipu la tin g exter n a l da ta r esou r ces.
System .Ru n tim e.I n ter opSer vices En a blin g m a n a ged code to a ccess u n m a n a ged OS pla tfor m fa cilities, su ch a s
COM com pon en ts a n d fu n ction s in Win 32 DLLs.
System .Ru n tim e.Rem otin g Accessin g types r em otely.
System .Ru n tim e.Ser ia liza tion En a blin g in sta n ces of objects to be per sisted a n d r egen er a ted fr om a str ea m .
System .Secu r ity Pr otectin g da ta a n d r esou r ces.
System .Text Wor kin g with text in differ en t en codin gs, like ASCI I or Un icode.
System .Th r ea din g Per for m in g a syn ch r on ou s oper a tion s a n d syn ch r on izin g a ccess to r esou r ces.
System .Xm l Pr ocessin g XML sch em a s a n d da ta .
Th is book is a bou t th e CLR a n d a bou t th e gen er a l types th a t in ter a ct closely with th e CLR ( wh ich wou ld in clu de m ost of
th e n a m espa ces listed a bove) . Th is m ea n s th a t th e con ten t of th is book is a pplica ble to a ll .NET Fr a m ewor k
pr ogr a m m er s r ega r dless of th e type of a pplica tion th ey' r e bu ildin g.
You sh ou ld be a wa r e, h owever , th a t in a ddition to su pplyin g th e m or e gen er a l n a m espa ces, th e FCL offer s n a m espa ces
wh ose types a r e u sed for bu ildin g specific a pplica tion types. Th e ta ble below lists som e of th e a pplica tion - specific
n a m espa ces:
Namespace Purpose of Types
System .Web.Ser vices Bu ildin g web ser vices.
System .Web.UI Bu ildin g web for m s.
System .Win dows.For m s Bu ildin g Win dows GUI a pplica tion s.
System .Ser vicePr ocess Bu ildin g a Win dows ser vice con tr olla ble by th e Ser vice Con tr ol Ma n a ger .
I expect m a n y good books will be pu blish ed th a t expla in h ow to bu ild specific a pplica tion types ( su ch a s Win dows
ser vices, web for m s, or Win dows for m s) . Th ese books will give you a n excellen t sta r t a t bu ildin g you r a pplica tion . I ten d
to th in k of th ese a pplica tion - specific books a s h elpin g you lea r n fr om th e top down beca u se th ey con cen tr a te on th e
a pplica tion type a n d n ot on th e developm en t pla tfor m . I t is m y in ten t th a t th is book offer in for m a tion th a t will h elp you
lea r n fr om th e bottom u p. Th e two types of books sh ou ld com plem en t ea ch oth er : After r ea din g th is book a n d a n
Applied .NET Framework Programming
16
a pplica tion - specific book, you sh ou ld be a ble to ea sily a n d pr oficien tly bu ild a n y kin d of .NET Fr a m ewor k a pplica tion
you desir e.
The Common Type System
By n ow, it sh ou ld be obviou s to you th a t th e CLR is a ll a bou t types. Types expose fu n ction a lity to you r a pplica tion s a n d
com pon en ts. Types a r e th e m ech a n ism by wh ich code wr itten in on e pr ogr a m m in g la n gu a ge ca n ta lk to code wr itten in
a differ en t pr ogr a m m in g la n gu a ge. Beca u se types a r e a t th e r oot of th e CLR, Micr osoft cr ea ted a for m a l specifica tion
th e com m on type system ( CTS) th a t descr ibes h ow types a r e defin ed a n d beh a ve.
Th e CTS specifica tion sta tes th a t a type m a y con ta in zer o or m or e m em ber s. I n Ch a pter 7, Type Mem ber Accessibility,
I ' ll discu ss a ll th ese m em ber s in gr ea t deta il. For n ow, I ju st wa n t to give you a br ief in tr odu ction to th em :
Field. A da ta va r ia ble th a t is pa r t of th e objects sta te. Fields a r e iden tified by th eir n a m e a n d type.
Method. A fu n ction th a t per for m s a n oper a tion on th e object, often ch a n gin g th e objects sta te. Meth ods
h a ve a n a m e, sign a tu r e, a n d m odifier s. Th e sign a tu r e specifies th e ca llin g con ven tion , n u m ber of
pa r a m eter s ( a n d th eir sequ en ce) , th e types of th e pa r a m eter s, a n d th e type of va lu e retu rn ed by th e
m eth od.
Pr oper ty. To th e ca ller , th is m em ber looks like a field. Bu t to th e type im plem en ter , th is m em ber looks
like a m eth od ( or two) . Pr oper ties a llow a n im plem en ter to va lida te in pu t pa r a m eter s a n d object sta te
befor e a ccessin g th e va lu e a n d to ca lcu la te a va lu e on ly wh en n ecessa r y; th ey a lso a llow a u ser of th e type
to h a ve sim plified syn ta x. Fin a lly, pr oper ties a lso a llow you to cr ea te r ea d- on ly or wr ite- on ly fields.
Event. A n otifica tion m ech a n ism between a n object a n d oth er in ter ested objects. For exa m ple, a bu tton
cou ld offer a n even t th a t n otifies oth er objects wh en th e bu tton is clicked.
Th e CTS a lso specifies th e r u les for type visibility a n d for a ccess to th e m em ber s of a type. For exa m ple, m a r kin g a type a s
public expor ts th e type, m a kin g it visible a n d a ccessible to a n y a ssem bly. On th e oth er h a n d, m a r kin g a type a s
assembly ( ca lled internal in C#) m a kes th e type visible a n d a ccessible to code with in th e sa m e a ssem bly
on ly. Th u s, th e CTS esta blish es th e r u les by wh ich a ssem blies for m a bou n da r y of visibility for a type, a n d th e r u n tim e
en for ces th e visibility r u les.
Rega r dless of wh eth er a type is visible to a ca ller , th e type gets to con tr ol wh eth er th e ca ller h a s a ccess to its m em ber s.
Th e followin g list sh ows th e va lid option s for con tr ollin g a ccess to a m eth od or field:
Pr ivate. Ca lla ble on ly by oth er m eth ods in th e sa m e cla ss type.
Family. Ca lla ble by der ived types, r ega r dless of wh eth er th ey a r e with in th e sa m e a ssem bly. Note th a t
m a n y la n gu a ges ( like C+ + a n d C#) r efer to family a s protected.
Family and Assembly. Ca lla ble by der ived types, bu t on ly if th e der ived type is defin ed in th e sa m e
a ssem bly.
Assembly. Ca lla ble by a n y code in th e sa m e a ssem bly. Note th a t m a n y la n gu a ges r efer to
assembly a s internal.
Family or Assembly. Ca lla ble by der ived types in a n y a ssem bly a n d by a n y types in th e sa m e
a ssem bly. Note th a t C# r efer s to family or assembly a s protected internal.
Architecture of the .NET Framework
17
Public. Ca lla ble by a n y code in a n y a ssem bly.
I n a ddition , th e CTS defin es th e r u les gover n in g type in h er ita n ce, vir tu a l fu n ction s, object lifetim e, a n d so on . Th ese
r u les h a ve been design ed to a ccom m oda te th e sem a n tics expr essible in m oder n pr ogr a m m in g la n gu a ges. I n fa ct, you
won t even n eed to lea r n th e CTS r u les, per se, beca u se th e la n gu a ge you u se exposes its own la n gu a ge syn ta x a n d type
r u les in th e sa m e wa y you a r e fa m ilia r with toda y; it m a ps th e la n gu a ge- specific syn ta x in to th e "la n gu a ge" of th e CLR
wh en it em its th e m a n a ged m odu le.
Wh en I fir st sta r ted wor kin g with th e CLR, I soon r ea lized th a t it is best to th in k of th e la n gu a ge a n d th e beh a vior of you r
code a s two sepa r a te a n d distin ct th in gs. Usin g C+ + , you ca n defin e you r own types with th eir own m em ber s. Of cou r se,
you cou ld h a ve u sed C# or Visu a l Ba sic.NET to defin e th e sa m e type with th e sa m e m em ber s. Su r e, th e syn ta x you u se for
defin in g th is type is differ en t depen din g on th e la n gu a ge you ch oose, bu t th e beh a vior of th e type will be a bsolu tely
iden tica l r ega r dless of th e la n gu a ge beca u se th e CLR by m ea n s of th e CTS defin es th e beh a vior of th e type.
To h elp m a ke th is clea r , let m e give you a n exa m ple: Th e CTS su ppor ts sin gle in h er ita n ce on ly. Th e C+ + la n gu a ge
su ppor ts types th a t in h er it fr om m u ltiple ba se types; n ever th eless, th e CTS ca n n ot a ccept a n d oper a te on a n y su ch type.
To h elp you , th e Visu a l C+ + com piler r epor ts a n er r or if it detects th a t you r e a ttem ptin g to cr ea te m a n a ged code th a t
in clu des a type in h er ited fr om m u ltiple ba se types.
Her es a n oth er CTS r u le: All types m u st ( u ltim a tely) in h er it fr om a pr edefin ed type, System.Object. As you ca n
see, Object is th e n a m e of a type defin ed in th e System n a m espa ce. Th is Object is th e r oot of a ll oth er types
a n d th er efor e gu a r a n tees ever y type in sta n ce h a s a m in im u m set of beh a vior s. Specifica lly, th e System.Object
type a llows you to:
com pa r e two in sta n ces for equ a lity
obta in a h a sh code for th e in sta n ce
qu er y th e tr u e type of a n in sta n ce
per for m a sh a llow ( bitwise) copy of th e in sta n ce
obta in a str in g r epr esen ta tion of th e in sta n ce' s objects cu r r en t sta te
The Common Language Specification
COM a llows objects cr ea ted in differ en t la n gu a ges to com m u n ica te with on e a n oth er . Th e CLR goes fu r th er . I t in tegr a tes
a ll la n gu a ges to let objects cr ea ted in on e la n gu a ge be tr ea ted a s equ a l citizen s by code wr itten in a com pletely differ en t
la n gu a ge. To m a ke th is possible, th e CLR defin es a sta n da r d beh a vior for types, em beds self- descr ibin g type in for m a tion
( m eta da ta ) , a n d pr ovides a com m on execu tion en vir on m en t.
La n gu a ge in tegr a tion is a fa n ta stic goa l, of cou r se, bu t th e tr u th of th e m a tter is th a t pr ogr a m m in g la n gu a ges a r e ver y
differ en t fr om on e a n oth er . For exa m ple, som e la n gu a ges la ck fea tu r es com m on ly u sed in oth er la n gu a ges:
ca se- sen sitivity
u n sign ed in teger s
oper a tor over loa din g
m eth ods th a t su ppor t a va r ia ble n u m ber of pa r a m eter s
Applied .NET Framework Programming
18
I f you in ten d to cr ea te types th a t a r e ea sily a ccessible fr om oth er pr ogr a m m in g la n gu a ges, th en it is im por ta n t th a t you
u se on ly fea tu r es of you r pr ogr a m m in g la n gu a ge th a t a r e gu a r a n teed to be a va ila ble in a ll oth er la n gu a ges. To h elp you
with th is, Micr osoft h a s defin ed a common languagespecification ( CLS) th a t deta ils for com piler ven dor s th e
m in im u m set of fea tu r es th a t th eir com piler s m u st su ppor t if th ey a r e to ta r get th e r u n tim e.
Note th a t th e CLR/ CTS su ppor ts a lot m or e fea tu r es th a n th e su bset defin ed by th e com m on la n gu a ge specifica tion , so if
you don t ca r e a bou t la n gu a ge in ter oper a bility, you ca n develop ver y r ich types lim ited on ly by th e ca pa bilities of th e
la n gu a ge. Specifica lly, th e CTS defin es r u les to wh ich exter n a lly visible types a n d m eth ods m u st a dh er e if th ey a r e to be
a ccessible fr om a n y CLR- com plia n t pr ogr a m m in g la n gu a ge. Note th a t th e CLS r u les do n ot a pply to code th a t is on ly
a ccessible with in th e defin in g a ssem bly. Th e figu r e below su m m a r izes th e wa y in wh ich la n gu a ge fea tu r es over la p with
th e CLS, with in th e wider con text of th e CLR/ CTS.

As th is figu r e sh ows, th e CLR/ CTS offer s a br oa dly in clu sive set of fea tu r es. Som e la n gu a ges expose a la r ge su bset of th e
CLR/ CTS; in fa ct, A pr ogr a m m er willin g to wr ite in I L a ssem bly la n gu a ge is a ble to u se a ll th e fea tu r es offer ed by th e
CLR/ CTS. Most oth er la n gu a ges su ch a s C#, VB, a n d For tr a n expose a su bset of th e CLR/ CTS fea tu r es to th e
pr ogr a m m er . Th e CLS defin es a m in im u m set of fea tu r es th a t a ll la n gu a ges m u st su ppor t.
I f you a r e design in g a type in on e la n gu a ge a n d you expect th a t type to be u sed by a n oth er la n gu a ge, th en you sh ou ld
n ot ta ke a dva n ta ge of a n y fea tu r es th a t a r e ou tside of th e CLS. Doin g so m ea n s th a t you r type m em ber s m igh t n ot be
a ccessible by pr ogr a m m er s wr itin g code in oth er pr ogr a m m in g la n gu a ges.
I n th e code below, a CLS- com plia n t type is bein g defin ed in C#. However , th e type h a s a few n on - CLS- com plia n t
con str u cts th a t ca u se th e C# com piler to com pla in a bou t th e code:
using System;

// Tell compiler to check for CLS Compliance
[assembly:CLSCompliant(true)]

// Errors appear because the class is public
public class App {

// Error: Return type of 'App.Abc()' is not CLS-compliant
Architecture of the .NET Framework
19
public UInt32 Abc() { return 0; }

// Error: Identifier 'App.abc()' differing
// only in case is not CLS-compliant
public void abc() { }

// No error: Method is private
private UInt32 ABC() { return 0; }
}
I n th e code a bove, th e [assembly:CLSCompliant(true)] a ttr ibu te is a pplied to th e a ssem bly. Th is
a ttr ibu te tells th e com piler to en su r e th a t a n y pu blicly exposed type h a s n o con str u ct th a t wou ld pr even t th e type fr om
bein g a ccessed fr om a n y oth er pr ogr a m m in g la n gu a ge. Wh en th e code a bove is com piled, th e C# com piler em its two
er r or s. Th e fir st er r or is r epor ted beca u se th e m eth od Abc r etu r n s a n u n sign ed in teger ; Visu a l Ba sic.NET a n d som e
oth er la n gu a ges ca n n ot m a n ipu la te u n sign ed in teger va lu es. Th e secon d er r or a r ises beca u se th is type exposes two
pu blic m eth ods wh ich differ on ly by ca se: Abc a n d abc. Visu a l Ba sic.NET a n d som e oth er la n gu a ges ca n n ot ca ll both
of th ese m eth ods.
Note th a t if you wer e to delete public fr om in fr on t of class App a n d r ecom pile, both er r or s wou ld go a wa y.
Th e r ea son is th a t th e App type wou ld defa u lt to internal a n d wou ld th er efor e n o lon ger be exposed ou tside th e
a ssem bly. For a com plete list of CLS r u les, r efer to th e "Cr oss- La n gu a ge I n ter oper a bility" section in th e .NET Fr a m ewor k
SDK docu m en ta tion .
Let m e distill th e CLS r u les to som eth in g ver y sim ple. I n th e CLR, ever y m em ber of a type is eith er a field ( da ta ) or a
m eth od ( beh a vior ) . Th is m ea n s th a t ever y pr ogr a m m in g la n gu a ge m u st be a ble to a ccess fields a n d ca ll m eth ods.
Cer ta in fields a n d cer ta in m eth ods a r e u sed in specia l a n d com m on wa ys. To m a ke codin g th ese com m on pr ogr a m m in g
pa tter n s ea sier , la n gu a ges typica lly offer a ddition a l a bstr a ction s. For exa m ple, la n gu a ges expose con cepts su ch a s
en u m s, a r r a ys, pr oper ties, in dexer s, delega tes, even ts, con str u ctor s, destr u ctor s, oper a tor over loa ds, a n d con ver sion
oper a tor s. Wh en com piler s com e a cr oss a n y of th ese con str u cts in you r sou r ce code, th e com piler m u st tr a n sla te th em
in to fields a n d m eth ods so th a t th e CLR ( a n d a n y oth er pr ogr a m m in g la n gu a ge) ca n a ccess th e con str u ct.
Con sider th e followin g type defin ition th a t con ta in s a con str u ctor , a destr u ctor , som e over loa ded oper a tor s, a pr oper ty,
a n in dexer , a n d a n even t. Note th a t a ddition a l code is in tr odu ced to m a ke th e code com pile; th e code does n ot illu str a te
th e cor r ect wa y to im plem en t a type.
using System;

class Test {
// Constructor
public Test() {}

// Destructor
~Test() {}

// Operator overload
public static Boolean operator == (Test t1, Test t2) {
return true;
}
public static Boolean operator != (Test t1, Test t2) {
return false;
}
Applied .NET Framework Programming
20

// An operator overload
public static Test operator + (Test t1, Test t2) { return null; }

// A Property
public String AProperty {
get { return null; }
set { }
}

// An Indexer
public String this[Int32 x] {
get { return null; }
set { }
}

// An Event
event EventHandler AnEvent;
}
Wh en th e com piler com piles th is code, th e r esu lt is a type th a t h a s a n u m ber of fields a n d m eth ods defin ed in it. You ca n
ea sily see th is u sin g th e I L disa ssem bler tool ( I LDa sm .exe) pr ovided with th e .NET Fr a m ewor k SDK to exa m in e th e
r esu ltin g m a n a ged m odu le:

Th e ta ble on th e n ext pa ge sh ows h ow th e pr ogr a m m in g la n gu a ge con str u cts got m a pped to th e equ iva len t CLR fields
a n d m eth ods:

Architecture of the .NET Framework
21
Type Member Member
Type
Equivalent Programming
Language Construct
An Even t Field Even t. Th e n a m e of th e field is An Even t a n d its type is
System .Even tHa n dler
.ctor Meth od Con str u ctor
Fin a lize Meth od Destr u ctor
a dd_ An Even t Meth od Even t a dd a ccessor m eth od
get_ APr oper ty Meth od Pr oper ty get a ccessor m eth od
get_ I tem Meth od I n dexer get a ccessor m eth od
op_ Addition Meth od + oper a tor
op_ Equ a lity Meth od == oper a tor
op_ I n equ a lity Meth od != oper a tor
r em ove_ An Even t Meth od Even t r em ove a ccessor m eth od
set_ APr oper ty Meth od Pr oper ty set a ccessor m eth od
set_ I tem Meth od I n dexer set a ccessor m eth od
Th e a ddition a l n odes u n der th e Test type th a t a r e n ot m en tion ed in th e ta ble a bove .cla ss, .cu stom , An Even t,
APr oper ty, a n d I tem iden tify a ddition a l m eta da ta a bou t th e type. Th ese n odes do n ot m a p to fields or m eth ods; th ey
ju st offer som e a ddition a l in for m a tion a bou t th e type th a t th e CLR, pr ogr a m m in g la n gu a ges, or tools ca n get a ccess to.
For exa m ple, a tool ca n see th a t th e Test type offer s a n even t, ca lled AnEvent, wh ich is exposed via th e two
m eth ods ( add_AnEvent a n d remove_AnEvent) .
Interoperability with Unmanaged Code
Th e .NET Fr a m ewor k offer s a ton of a dva n ta ges over oth er developm en t pla tfor m s. However , ver y few com pa n ies ca n
a ffor d to r edesign a n d r e- im plem en t a ll th eir existin g code. Micr osoft r ea lizes th is a n d h a s bu ilt th e CLR so th a t it offer s
m ech a n ism s th a t a llow a n a pplica tion to con sist of both m a n a ged a n d u n m a n a ged pa r ts. Specifica lly, th e CLR su ppor ts
th r ee in ter oper a bility scen a r ios:
Managed code can call an unmanaged function in a DLL. Ma n a ged code ca n ea sily ca ll
fu n ction s con ta in ed in DLLs u sin g a m ech a n ism ca lled P/ I n voke ( for Pla tfor m I n voke) . After a ll, m a n y
of th e types defin ed in th e FCL in ter n a lly ca ll fu n ction s expor ted fr om Ker n el32.dll, User 32.dll, a n d so on .
Ma n y pr ogr a m m in g la n gu a ges will expose a m ech a n ism th a t m a kes it ea sy for m a n a ged code to ca ll ou t
to u n m a n a ged fu n ction s con ta in ed in DLLs.
Exa m ple: A C# or VB a pplica tion ca n ca ll th e Cr ea teSem a ph or e fu n ction expor ted fr om Ker n el32.dll.
Managed code can use an existing COM component ( ser ver ) . Ma n y com pa n ies h a ve a lr ea dy
im plem en ted a n u m ber of u n m a n a ged COM com pon en ts. Usin g th e type libr a r y fr om th ese com pon en ts,
a m a n a ged a ssem bly ca n be cr ea ted th a t descr ibes th e COM com pon en t. Ma n a ged code ca n a ccess th e
type in th e m a n a ged a ssem bly ju st like a n y oth er m a n a ged type. See th e TlbI m p.exe tool th a t sh ips with
th e .NET Fr a m ewor k SDK for m or e in for m a tion . I f you do n ot h a ve a type libr a r y or you wa n t to h a ve
m or e con tr ol over wh a t TlbI m p.exe pr odu ces, you ca n m a n u a lly bu ild a type in sou r ce code th a t th e CLR
ca n u se for doin g th e pr oper in ter op.
Exa m ple: Usin g Dir ectX COM com pon en ts fr om a C# or VB a pplica tion .
Applied .NET Framework Programming
22
Unmanaged code can use a managed type ( ser ver ) . A lot of existin g u n m a n a ged code r equ ir es
th a t you su pply a COM com pon en t for th e code to wor k cor r ectly. You ca n im plem en t th ese com pon en ts
ea sily u sin g m a n a ged code, a voidin g a ll th e code th a t h a s to do with r efer en ce cou n tin g a n d in ter fa ces.
See th e TlbExp.exe a n d RegAsm .exe tools th a t sh ip with th e .NET Fr a m ewor k SDK for m or e in for m a tion .
Exa m ple: Cr ea tin g a n ActiveX con tr ol or a sh ell exten sion in C# or VB.
I n a ddition to th e a bove, th e Micr osoft Visu a l C+ + com piler ( ver sion 13) su ppor ts a n ew / clr com m a n d- lin e switch . Th is
switch tells th e com piler to em it I L code in stea d of n a tive x86 in str u ction s. I f you h a ve a la r ge a m ou n t of existin g C+ +
code, you ca n r ecom pile th e code u sin g th is n ew com piler switch . Th e n ew code will r equ ir e th e CLR to execu te, a n d you
ca n m odify th e code over tim e to ta ke a dva n ta ge of th e CLR- specific fea tu r es.
Note th a t th e / clr switch ca n n ot com pile to I L a n y m eth od th a t does on e of th e followin g:
con ta in s in lin e a ssem bly la n gu a ge ( via th e __asm keywor d) ,
a ccepts a va r ia ble n u m ber of a r gu m en ts,
ca lls setjm p,
con ta in s in tr in sic r ou tin es ( su ch a s __enable, __disable, _ReturnAddress, a n d
_AddressOfReturnAddress) .
For a com plete list of th e con str u cts th a t th e C+ + com piler ca n n ot com pile in to I L, see th e docu m en ta tion for th e Visu a l
C+ + com piler . Wh en th e com piler ca n t com pile th e m eth od in to I L, it com piles th e m eth od in to x86 so th a t th e
a pplica tion still r u n s.
Note fu r th er th a t a lth ou gh th e I L code pr odu ced is m a n a ged, th e da ta is n ot m a n a ged. Th a t is, da ta objects a r e n ot
a lloca ted fr om th e m a n a ged h ea p, a n d th ey a r e n ot ga r ba ge collected. I n fa ct, th e da ta types do n ot h a ve m eta da ta
pr odu ced for th em a n d th e type m eth od n a m es a r e m a n gled.
Th e C code below ca lls th e sta n da r d C r u n tim e libr a r y printf fu n ction a n d a lso ca lls th e WriteLine m eth od
in System.Console. Note th a t th e System.Console type is defin ed in th e Fr a m ewor k Cla ss Libr a r y.
Accor din gly, C/ C+ + code ca n u se libr a r ies a va ila ble to C/ C+ + a s well a s m a n a ged types.
#include <stdio.h> // For printf

#using <mscorlib.dll> // For managed types defined in this assembly
using namespace System; // Easily access System namespace types

// Implement a normal C/C++ main function
void main () {

// Call the C-Runtime library's printf function
printf("Displayed by printf.\r\n");

// Call System.Console's WriteLine method
Console::WriteLine("Displayed by Console::WriteLine.");
}
Com pilin g th is code cou ldn ' t be ea sier . I f th e code a bove wer e pla ced in a Test.cpp file, you ' d com pile it by execu tin g th e
followin g lin e a t th e com m a n d pr om pt:
cl /clr Test.cpp
Architecture of the .NET Framework
23
Th e r esu lt is a Test.exe a ssem bly file. I f you r u n Test.exe, you ' ll see th e followin g ou tpu t:
C:\>Test
Displayed by printf.
Displayed by Console::WriteLine.
I f you u se I LDa sm to exa m in e th is file, you ll see th e followin g:

Her e, I LDa sm sh ows a ll th e globa l fu n ction s a n d globa l fields defin ed with in th e a ssem bly. Obviou sly, th e com piler h a s
gen er a ted a lot of stu ff a u tom a tica lly. I f you dou ble- click th e Main m eth od, I LDa sm will sh ow you th e I L code:
.method public static int32
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
main() cil managed
{
.vtentry 1 : 1
// Code size 28 (0x1c)
.maxstack 1
IL_0000: ldsflda valuetype
$ArrayType$0x0faed885 '?A0x44d29f64.unnamed-global-0'
IL_0005: call vararg int32
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
printf(int8
modopt([Microsoft.VisualC]Microsoft.VisualC.NoSignSpecifiedModifier)
modopt([Microsoft.VisualC]Microsoft.VisualC.IsConstModifier)*)
IL_000a: pop
IL_000b: ldsflda valuetype
$ArrayType$0x0e6cb2b2 '?A0x44d29f64.unnamed-global-1'
IL_0010: newobj instance void [mscorlib]System.String::.ctor(int8*)
IL_0015: call void [mscorlib]System.Console::WriteLine(string)
IL_001a: ldc.i4.0
Applied .NET Framework Programming
24
IL_001b: ret
} // end of method 'Global Functions'::main
Wh a t we see h er e isn t pr etty beca u se th e com piler gen er a tes a lot of specia l code to m a ke a ll th is wor k. You ca n see,
h owever , th a t th e I L a bove m a kes ca lls to both printf a n d th e WriteLine m eth od in Console.
Architecture of the .NET Framework
25

Das könnte Ihnen auch gefallen