Sunteți pe pagina 1din 29

HOME .NET FAQ C# FAQ About me andy@andymcm.

com
.NET Framework Frequently Asked Questions
Andy McMullan
Last update: 11Feb!""#
Contents
1. $nt%oduct&on
o 1.1 '(at &s .NET)
o 1.! *oes .NET only apply to people bu&ld&n+ ,ebs&tes)
o 1.# '(en ,as .NET announced)
o 1.- '(en ,as t(e .&%st /e%s&on o. .NET %eleased)
o 1.0 '(at tools can $ use to de/elop .NET appl&cat&ons)
o 1.1 '(at plat.o%ms does t(e .NET F%ame,o%2 %un on)
o 1.3 '(at lan+ua+es does t(e .NET F%ame,o%2 suppo%t)
o 1.4 '&ll t(e .NET F%ame,o%2 +o t(%ou+( a standa%d&sat&on p%ocess)

!. 5as&c te%m&nolo+y
o !.1 '(at &s t(e CL6)
o !.! '(at &s t(e CT7)
o !.# '(at &s t(e CL7)
o !.- '(at &s $L)
o !.0 '(at &s C#)
o !.1 '(at does 8mana+ed8 mean &n t(e .NET conte9t)
o !.3 '(at &s %e.lect&on)

#. Assembl&es
o #.1 '(at &s an assembly)
o #.! Ho, can $ p%oduce an assembly)
o #.# '(at &s t(e d&..e%ence bet,een a p%&/ate assembly and a s(a%ed assembly)
o #.- Ho, do assembl&es .&nd eac( ot(e%)
o #.0 Ho, does assembly /e%s&on&n+ ,o%2)

-. Appl&cat&on *oma&ns
o -.1 '(at &s an Appl&cat&on *oma&n)
o -.! Ho, does an App*oma&n +et c%eated)
o -.# Can $ ,%&te my o,n .NET (ost)

0. :a%ba+e Collect&on
o 0.1 '(at &s +a%ba+e collect&on)
o 0.! $s &t t%ue t(at ob;ects don8t al,ays +et dest%oyed &mmed&ately ,(en t(e last %e.e%ence +oes a,ay)
o 0.# '(y doesn8t t(e .NET %unt&me o..e% dete%m&n&st&c dest%uct&on)
o 0.- $s t(e lac2 o. dete%m&n&st&c dest%uct&on &n .NET a p%oblem)
o 0.0 *oes nondete%m&n&st&c dest%uct&on a..ect t(e usa+e o. COM ob;ects .%om mana+ed code)
o 0.1 $8/e (ea%d t(at F&nal&<e met(ods s(ould be a/o&ded. 7(ould $ &mplement F&nal&<e on my class)
o 0.3 *o $ (a/e any cont%ol o/e% t(e +a%ba+e collect&on al+o%&t(m)
o 0.4 Ho, can $ .&nd out ,(at t(e +a%ba+e collecto% &s do&n+)

1. 7e%&al&<at&on
o 1.1 '(at &s se%&al&<at&on)
o 1.! *oes t(e .NET F%ame,o%2 (a/e &nbu&lt suppo%t .o% se%&al&<at&on)
o 1.# $ ,ant to se%&al&<e &nstances o. my class. 7(ould $ use =ml7e%&al&<e%> 7oapFo%matte% o%
5&na%yFo%matte%)
o 1.- Can $ custom&se t(e se%&al&<at&on p%ocess)
o 1.0 '(y &s =ml7e%&al&<e% so slo,)
o 1.1 '(y do $ +et e%%o%s ,(en $ t%y to se%&al&<e a Has(table)
o 1.3 =ml7e%&al&<e% &s t(%o,&n+ a +ene%&c ?T(e%e ,as an e%%o% %e.lect&n+ MyClass? e%%o%. Ho, do $ .&nd out
,(at t(e p%oblem &s)

3. Att%&butes
o 3.1 '(at a%e att%&butes)
o 3.! Can $ c%eate my o,n metadata att%&butes)
o 3.# Can $ c%eate my o,n conte9t att%&butes)

4. Code Access 7ecu%&ty
o 4.1 '(at &s Code Access 7ecu%&ty @CA7A)
o 4.! Ho, does CA7 ,o%2)
o 4.# '(o de.&nes t(e CA7 code +%oups)
o 4.- Ho, do $ de.&ne my o,n code +%oup)
o 4.0 Ho, do $ c(an+e t(e pe%m&ss&on set .o% a code +%oup)
o 4.1 Can $ c%eate my o,n pe%m&ss&on set)
o 4.3 $8m (a/&n+ some t%ouble ,&t( CA7. Ho, can $ d&a+nose my p%oblem)
o 4.4 $ can8t be bot(e%ed ,&t( all t(&s CA7 stu... Can $ tu%n &t o..)

B. $nte%med&ate Lan+ua+e @$LA
o B.1 Can $ loo2 at t(e $L .o% an assembly)
o B.! Can sou%ce code be %e/e%seen+&nee%ed .%om $L)
o B.# Ho, can $ stop my code be&n+ %e/e%seen+&nee%ed .%om $L)
o B.- Can $ ,%&te $L p%o+%ams d&%ectly)
o B.0 Can $ do t(&n+s &n $L t(at $ can8t do &n C#)

1". $mpl&cat&ons .o% COM
o 1".1 $s COM dead)
o 1".! $s *COM dead)
o 1".# $s MT7CCOMD dead)
o 1".- Can $ use COM components .%om .NET p%o+%ams)
o 1".0 Can $ use .NET components .%om COM p%o+%ams)
o 1".1 $s ATL %edundant &n t(e .NET ,o%ld)

11. M&scellaneous
o 11.1 Ho, does .NET %emot&n+ ,o%2)
o 11.! Ho, can $ +et at t(e '&n#! AE$ .%om a .NET p%o+%am)

1!. Class L&b%a%y
o 1!.1 F&le $CO
1!.1.1 Ho, do $ %ead .%om a te9t .&le)
1!.1.! Ho, do $ ,%&te to a te9t .&le)
1!.1.# Ho, do $ %eadC,%&te b&na%y .&les)
1!.1.- Ho, do $ delete a .&le)
o 1!.! Te9t E%ocess&n+
1!.!.1 A%e %e+ula% e9p%ess&ons suppo%ted)
o 1!.# $nte%net
1!.#.1 Ho, do $ do,nload a ,eb pa+e)
1!.#.! Ho, do $ use a p%o9y)
o 1!.- =ML
1!.-.1 $s *OM suppo%ted)
1!.-.! $s 7A= suppo%ted)
1!.-.# $s =Eat( suppo%ted)
o 1!.0 T(%ead&n+
1!.0.1 $s mult&t(%ead&n+ suppo%ted)
1!.0.! Ho, do $ spa,n a t(%ead)
1!.0.# Ho, do $ stop a t(%ead)
1!.0.- Ho, do $ use t(e t(%ead pool)
1!.0.0 Ho, do $ 2no, ,(en my t(%ead pool ,o%2 &tem (as completed)
1!.0.1 Ho, do $ p%e/ent concu%%ent access to my data)
o 1!.1 T%ac&n+
1!.1.1 $s t(e%e bu&lt&n suppo%t .o% t%ac&n+Clo++&n+)
1!.1.! Can $ %ed&%ect t%ac&n+ to a .&le)
1!.1.# Can $ custom&se t(e t%ace output)

1#. 6esou%ces
o 1#.1 6ecommended boo2s
o 1#.! $nte%net 6esou%ces
o 1#.# 'eblo+s
o 1#.- 7ample code F ut&l&t&es
1. Introduction
1.1 What is .NET?
T(at8s d&..&cult to sum up &n a sentence. Acco%d&n+ to M&c%oso.t> .NET &s a ?%e/olut&ona%y ne, plat.o%m> bu&lt on open
$nte%net p%otocols and standa%ds> ,&t( tools and se%/&ces t(at meld comput&n+ and commun&cat&ons &n ne, ,ays?.
A mo%e p%act&cal de.&n&t&on ,ould be t(at .NET &s a ne, en/&%onment .o% de/elop&n+ and %unn&n+ so.t,a%e appl&cat&ons>
.eatu%&n+ ease o. de/elopment o. ,ebbased se%/&ces> %&c( standa%d %unt&me se%/&ces a/a&lable to components ,%&tten &n a
/a%&ety o. p%o+%amm&n+ lan+ua+es> and &nte%lan+ua+e and &nte%mac(&ne &nte%ope%ab&l&ty.
Note t(at ,(en t(e te%m ?.NET? &s used &n t(&s FAQ &t %e.e%s only to t(e ne, .NET %unt&me and assoc&ated tec(nolo+&es.
T(&s &s somet&mes called t(e ?.NET F%ame,o%2?. T(&s FAQ does NOT co/e% any o. t(e /a%&ous ot(e% e9&st&n+ and ne,
p%oductsCtec(nolo+&es t(at M&c%oso.t a%e attac(&n+ t(e .NET name to @e.+. 7QL 7e%/e%.NETA.
1. !oes .NET only a""ly to "eo"le #uildin$ we#%sites?
No. $. you ,%&te any '&ndo,s so.t,a%e @us&n+ ATLCCOM> MFC> G5> o% e/en %a, '&n#!A> .NET may o..e% a /&able
alte%nat&/e @o% add&t&onA to t(e ,ay you do t(&n+s cu%%ently. O. cou%se> &. you do de/elop ,eb s&tes> t(en .NET (as lots to
&nte%est you not least A7E.NET.
1.& When was .NET announced?
5&ll :ates del&/e%ed a 2eynote at Fo%um !"""> (eld Hune !!> !"""> outl&n&n+ t(e .NET 8/&s&on8. T(e Huly !""" E*C (ad a
numbe% o. sess&ons on .NET tec(nolo+y> and dele+ates ,e%e +&/en C*s conta&n&n+ a p%e%elease /e%s&on o. t(e .NET
.%ame,o%2C7*I and G&sual 7tud&o.NET.
1.' When was the (irst )ersion o( .NET released?
T(e .&nal /e%s&on o. t(e 1." 7*I and %unt&me ,as made publ&cly a/a&lable a%ound 1pm E7T on 10Han!""!. At t(e same
t&me> t(e .&nal /e%s&on o. G&sual 7tud&o.NET ,as made a/a&lable to M7*N subsc%&be%s.
1.* What tools can I use to de)elo" .NET a""lications?
T(e%e a%e a numbe% o. tools> desc%&bed (e%e &n ascend&n+ o%de% o. cost:
.NET F%ame,o%2 7*I : T(e 7*I &s .%ee and &ncludes commandl&ne comp&le%s .o% CDD> C#> and G5.NET and /a%&ous
ot(e% ut&l&t&es to a&d de/elopment.
A7E.NET 'eb Mat%&9 : T(&s &s a .%ee A7E.NET de/elopment en/&%onment .%om M&c%oso.t. As ,ell as a :J$
de/elopment en/&%onment> t(e do,nload &ncludes a s&mple ,eb se%/e% t(at can be used &nstead o. $$7 to (ost
A7E.NET apps. T(&s opens up A7E.NET de/elopment to use%s o. '&ndo,s =E Home Ed&t&on> ,(&c( cannot %un $$7.
M&c%oso.t G&sual C# .NET 7tanda%d Ed&t&on : T(&s &s a c(eap @a%ound K1""A /e%s&on o. G&sual 7tud&o l&m&ted to one
lan+ua+e and also ,&t( l&m&ted ,&<a%d suppo%t. Fo% e9ample> t(e%e8s no ,&<a%d suppo%t .o% class l&b%a%&es o% custom J$
cont%ols. Jse.ul .o% be+&nne%s to lea%n ,&t(> o% .o% sa//y de/elope%s ,(o can ,o%2 a%ound t(e de.&c&enc&es &n t(e
suppl&ed ,&<a%ds. As ,ell as C#> t(e%e a%e G5.NET and CDD /e%s&ons.
M&c%oso.t G&sual 7tud&o.NET E%o.ess&onal : $. you (a/e a l&cense .o% G&sual 7tud&o 1."> you can +et t(e up+%ade. At t(e
t&me o. ,%&t&n+ @Hun !""!A> M&c%oso.t &s o..e%&n+ a K#"" ma&l&n %ebate on t(e up+%ade> so &t8s a p%etty +ood deal. G&sual
7tud&o.NET &ncludes suppo%t .o% all t(e M7 lan+ua+es @C#> CDD> G5.NETA and (as e9tens&/e ,&<a%d suppo%t.
At t(e top end o. t(e p%&ce spect%um a%e t(e G&sual 7tud&o.NET Ente%p%&se and Ente%p%&se A%c(&tect ed&t&ons. T(ese o..e%
e9t%a .eatu%es suc( as G&sual 7ou%cesa.e @/e%s&on cont%olA> and pe%.o%mance and analys&s tools. C(ec2 out t(e G&sual
7tud&o.NET Featu%e Compa%&son at (ttp:CCmsdn.m&c%oso.t.comC/stud&oC(o,tobuyCc(oos&n+.asp.
1.+ What "lat(orms does the .NET Framework run on?
T(e %unt&me suppo%ts '&ndo,s =E> '&ndo,s !"""> NT- 7E1a and '&ndo,s MECB4. '&ndo,s B0 &s not suppo%ted. 7ome
pa%ts o. t(e .%ame,o%2 do not ,o%2 on all plat.o%ms .o% e9ample> A7E.NET &s only suppo%ted on '&ndo,s =E and
'&ndo,s !""". '&ndo,s B4CME cannot be used .o% de/elopment.
$$7 &s not suppo%ted on '&ndo,s =E Home Ed&t&on> and so cannot be used to (ost A7E.NET. Ho,e/e%> t(e A7E.NET 'eb
Mat%&9 ,eb se%/e% does %un on =E Home.
T(e Mono p%o;ect &s attempt&n+ to &mplement t(e .NET .%ame,o%2 on L&nu9.
1., What lan$ua$es does the .NET Framework su""ort?
M7 p%o/&des comp&le%s .o% C#> CDD> G5 and H7c%&pt. Ot(e% /endo%s (a/e announced t(at t(ey &ntend to de/elop .NET
comp&le%s .o% lan+ua+es suc( as CO5OL> E&..el> Ee%l> 7malltal2 and Eyt(on.
1.- Will the .NET Framework $o throu$h a standardisation "rocess?
F%om (ttp:CCmsdn.m&c%oso.t.comCnetCecmaC: ?On *ecembe% 1#> !""1> t(e ECMA :ene%al Assembly %at&.&ed t(e C# and
common lan+ua+e &n.%ast%uctu%e @CL$A spec&.&cat&ons &nto &nte%nat&onal standa%ds. T(e ECMA standa%ds ,&ll be 2no,n as
ECMA##- @C#A and ECMA##0 @t(e CL$A.?
. .asic terminolo$y
.1 What is the C/0?
CL6 L Common Lan+ua+e 6unt&me. T(e CL6 &s a set o. standa%d %esou%ces t(at @&n t(eo%yA any .NET p%o+%am can ta2e
ad/anta+e o.> %e+a%dless o. p%o+%amm&n+ lan+ua+e. 6obe%t 7c(m&dt @M&c%oso.tA l&sts t(e .ollo,&n+ CL6 %esou%ces &n (&s
M7*N E*C# a%t&cle:
Ob;ecto%&ented p%o+%amm&n+ model @&n(e%&tance> polymo%p(&sm> e9cept&on (andl&n+> +a%ba+e collect&onA
7ecu%&ty model
Type system
All .NET base classes
Many .NET .%ame,o%2 classes
*e/elopment> debu++&n+> and p%o.&l&n+ tools
E9ecut&on and code mana+ement
$Ltonat&/e t%anslato%s and opt&m&<e%s
'(at t(&s means &s t(at &n t(e .NET ,o%ld> d&..e%ent p%o+%amm&n+ lan+ua+es ,&ll be mo%e eMual &n capab&l&ty t(an t(ey
(a/e e/e% been be.o%e> alt(ou+( clea%ly not all lan+ua+es ,&ll suppo%t all CL6 se%/&ces.
. What is the CT1?
CT7 L Common Type 7ystem. T(&s &s t(e %an+e o. types t(at t(e .NET %unt&me unde%stands> and t(e%e.o%e t(at .NET
appl&cat&ons can use. Ho,e/e% note t(at not all .NET lan+ua+es ,&ll suppo%t all t(e types &n t(e CT7. T(e CT7 &s a
supe%set o. t(e CL7.
.& What is the C/1?
CL7 L Common Lan+ua+e 7pec&.&cat&on. T(&s &s a subset o. t(e CT7 ,(&c( all .NET lan+ua+es a%e e9pected to suppo%t.
T(e &dea &s t(at any p%o+%am ,(&c( uses CL7compl&ant types can &nte%ope%ate ,&t( any .NET p%o+%am ,%&tten &n any
lan+ua+e.
$n t(eo%y t(&s allo,s /e%y t&+(t &nte%op bet,een d&..e%ent .NET lan+ua+es .o% e9ample allo,&n+ a C# class to &n(e%&t .%om a
G5 class.
.' What is I/?
$L L $nte%med&ate Lan+ua+e. Also 2no,n as M7$L @M&c%oso.t $nte%med&ate Lan+ua+eA o% C$L @Common $nte%med&ate
Lan+ua+eA. All .NET sou%ce code @o. any lan+ua+eA &s comp&led to $L. T(e $L &s t(en con/e%ted to mac(&ne code at t(e
po&nt ,(e%e t(e so.t,a%e &s &nstalled> o% at %unt&me by a Hust$nT&me @H$TA comp&le%.
.* What is C2?
C# &s a ne, lan+ua+e des&+ned by M&c%oso.t to ,o%2 ,&t( t(e .NET .%ame,o%2. $n t(e&% ?$nt%oduct&on to C#? ,(&tepape%>
M&c%oso.t desc%&be C# as .ollo,s:
?C# &s a s&mple> mode%n> ob;ect o%&ented> and typesa.e p%o+%amm&n+ lan+ua+e de%&/ed .%om C and CDD. C# @p%onounced
NC s(a%pOA &s .&%mly planted &n t(e C and CDD .am&ly t%ee o. lan+ua+es> and ,&ll &mmed&ately be .am&l&a% to C and CDD
p%o+%amme%s. C# a&ms to comb&ne t(e (&+( p%oduct&/&ty o. G&sual 5as&c and t(e %a, po,e% o. CDD.?
7ubst&tute 8Ha/a8 .o% 8C#8 &n t(e Muote abo/e> and you8ll see t(at t(e statement st&ll ,o%2s p%etty ,ell :A.
$. you a%e a CDD p%o+%amme%> you m&+(t l&2e to c(ec2 out my C# FAQ.
.+ What does 3mana$ed3 mean in the .NET conte4t?
T(e te%m 8mana+ed8 &s t(e cause o. muc( con.us&on. $t &s used &n /a%&ous places ,&t(&n .NET> mean&n+ sl&+(tly d&..e%ent
t(&n+s.
Mana+ed code: T(e .NET .%ame,o%2 p%o/&des se/e%al co%e %unt&me se%/&ces to t(e p%o+%ams t(at %un ,&t(&n &t .o%
e9ample e9cept&on (andl&n+ and secu%&ty. Fo% t(ese se%/&ces to ,o%2> t(e code must p%o/&de a m&n&mum le/el o.
&n.o%mat&on to t(e %unt&me. 7uc( code &s called managed code. All C# and G&sual 5as&c.NET code &s mana+ed by de.ault.
G73 CDD code &s not mana+ed by de.ault> but t(e comp&le% can p%oduce mana+ed code by spec&.y&n+ a commandl&ne
s,&tc( @CcomDA.
Mana+ed data: T(&s &s data t(at &s allocated and deallocated by t(e .NET %unt&me8s +a%ba+e collecto%. C# and G5.NET
data &s al,ays mana+ed. G73 CDD data &s unmana+ed by de.ault> e/en ,(en us&n+ t(e CcomD s,&tc(> but &t can be
ma%2ed as mana+ed us&n+ t(e PP+c 2ey,o%d.
Mana+ed classes: T(&s &s usually %e.e%%ed to &n t(e conte9t o. Mana+ed E9tens&ons @MEA .o% CDD. '(en us&n+ ME CDD> a
class can be ma%2ed ,&t( t(e PP+c 2ey,o%d. As t(e name su++ests> t(&s means t(at t(e memo%y .o% &nstances o. t(e class
&s mana+ed by t(e +a%ba+e collecto%> but &t also means mo%e t(an t(at. T(e class becomes a .ully pa&dup membe% o.
t(e .NET commun&ty ,&t( t(e bene.&ts and %est%&ct&ons t(at b%&n+s. An e9ample o. a bene.&t &s p%ope% &nte%op ,&t( classes
,%&tten &n ot(e% lan+ua+es .o% e9ample> a mana+ed CDD class can &n(e%&t .%om a G5 class. An e9ample o. a %est%&ct&on &s
t(at a mana+ed class can only &n(e%&t .%om one base class.
., What is re(lection?
All .NET comp&le%s p%oduce metadata about t(e types de.&ned &n t(e modules t(ey p%oduce. T(&s metadata &s pac2a+ed
alon+ ,&t( t(e module @modules &n tu%n a%e pac2a+ed to+et(e% &n assembl&esA> and can be accessed by a mec(an&sm
called re(lection. T(e 7ystem.6e.lect&on namespace conta&ns classes t(at can be used to &nte%%o+ate t(e types .o% a
moduleCassembly.
Js&n+ %e.lect&on to access .NET metadata &s /e%y s&m&la% to us&n+ $TypeL&bC$Type$n.o to access type l&b%a%y data &n COM>
and &t &s used .o% s&m&la% pu%poses e.+. dete%m&n&n+ data type s&<es .o% ma%s(al&n+ data ac%oss conte9tCp%ocessCmac(&ne
bounda%&es.
6e.lect&on can also be used to dynam&cally &n/o2e met(ods @see 7ystem.Type.$n/o2eMembe%A> o% e/en c%eate types
dynam&cally at %unt&me @see 7ystem.6e.lect&on.Em&t.Type5u&lde%A.
&. Assem#lies
&.1 What is an assem#ly?
An assembly &s somet&mes desc%&bed as a lo+&cal .E=E o% .*LL> and can be an application @,&t( a ma&n ent%y po&ntA o% a
library. An assembly cons&sts o. one o% mo%e .&les @dlls> e9es> (tml .&les etcA> and %ep%esents a +%oup o. %esou%ces> type
de.&n&t&ons> and &mplementat&ons o. t(ose types. An assembly may also conta&n %e.e%ences to ot(e% assembl&es. T(ese
%esou%ces> types and %e.e%ences a%e desc%&bed &n a bloc2 o. data called a manifest. T(e man&.est &s pa%t o. t(e assembly>
t(us ma2&n+ t(e assembly sel.desc%&b&n+.
An &mpo%tant aspect o. assembl&es &s t(at t(ey a%e pa%t o. t(e &dent&ty o. a type. T(e &dent&ty o. a type &s t(e assembly t(at
(ouses &t comb&ned ,&t( t(e type name. T(&s means> .o% e9ample> t(at &. assembly A e9po%ts a type called T> and
assembly 5 e9po%ts a type called T> t(e .NET %unt&me sees t(ese as t,o completely d&..e%ent types. Fu%t(e%mo%e> don8t +et
con.used bet,een assembl&es and namespaces namespaces a%e me%ely a (&e%a%c(&cal ,ay o. o%+an&s&n+ type names.
To t(e %unt&me> type names a%e type names> %e+a%dless o. ,(et(e% namespaces a%e used to o%+an&se t(e names. $t8s t(e
assembly plus t(e typename @%e+a%dless o. ,(et(e% t(e type name belon+s to a namespaceA t(at un&Muely &ndent&.&es a
type to t(e %unt&me.
Assembl&es a%e also &mpo%tant &n .NET ,&t( %espect to secu%&ty many o. t(e secu%&ty %est%&ct&ons a%e en.o%ced at t(e
assembly bounda%y.
F&nally> assembl&es a%e t(e un&t o. /e%s&on&n+ &n .NET mo%e on t(&s belo,.
&. 5ow can I "roduce an assem#ly?
T(e s&mplest ,ay to p%oduce an assembly &s d&%ectly .%om a .NET comp&le%. Fo% e9ample> t(e .ollo,&n+ C# p%o+%am:
public class CTest
{
public CTest()
{
System.Console.WriteLine( "Hello from CTest" );
}
}
can be comp&led &nto a l&b%a%y assembly @dllA l&2e t(&s:
csc /tlibrary ctest.cs
Qou can t(en /&e, t(e contents o. t(e assembly by %unn&n+ t(e ?$L *&sassemble%? tool t(at comes ,&t( t(e .NET 7*I.
Alte%nat&/ely you can comp&le you% sou%ce &nto modules> and t(en comb&ne t(e modules &nto an assembly us&n+ t(e
assembly l&n2e% @al.e9eA. Fo% t(e C# comp&le%> t(e Cta%+et:module s,&tc( &s used to +ene%ate a module &nstead o. an
assembly.
&.& What is the di((erence #etween a "ri)ate assem#ly and a shared assem#ly?
/ocation and )isi#ility: A p%&/ate assembly &s no%mally used by a s&n+le appl&cat&on> and &s sto%ed &n t(e appl&cat&on8s
d&%ecto%y> o% a subd&%ecto%y beneat(. A s(a%ed assembly &s no%mally sto%ed &n t(e +lobal assembly cac(e> ,(&c( &s a
%epos&to%y o. assembl&es ma&nta&ned by t(e .NET %unt&me. 7(a%ed assembl&es a%e usually l&b%a%&es o. code ,(&c(
many appl&cat&ons ,&ll .&nd use.ul> e.+. t(e .NET .%ame,o%2 classes.

6ersionin$: T(e %unt&me en.o%ces /e%s&on&n+ const%a&nts only on s(a%ed assembl&es> not on p%&/ate assembl&es.
&.' 5ow do assem#lies (ind each other?
5y sea%c(&n+ d&%ecto%y pat(s. T(e%e a%e se/e%al .acto%s ,(&c( can a..ect t(e pat( @suc( as t(e App*oma&n (ost> and
appl&cat&on con.&+u%at&on .&lesA> but .o% p%&/ate assembl&es t(e sea%c( pat( &s no%mally t(e appl&cat&on8s d&%ecto%y and &ts
subd&%ecto%&es. Fo% s(a%ed assembl&es> t(e sea%c( pat( &s no%mally same as t(e p%&/ate assembly pat( plus t(e s(a%ed
assembly cac(e.
&.* 5ow does assem#ly )ersionin$ work?
Eac( assembly (as a /e%s&on numbe% called t(e compatibility /e%s&on. Also eac( %e.e%ence to an assembly @.%om anot(e%
assemblyA &ncludes bot( t(e name and /e%s&on o. t(e %e.e%enced assembly.
T(e /e%s&on numbe% (as .ou% nume%&c pa%ts @e.+. 0.0.!.##A. Assembl&es ,&t( e&t(e% o. t(e .&%st t,o pa%ts d&..e%ent a%e
no%mally /&e,ed as &ncompat&ble. $. t(e .&%st t,o pa%ts a%e t(e same> but t(e t(&%d &s d&..e%ent> t(e assembl&es a%e deemed
as 8maybe compat&ble8. $. only t(e .ou%t( pa%t &s d&..e%ent> t(e assembl&es a%e deemed compat&ble. Ho,e/e%> t(&s &s ;ust t(e
de.ault +u&del&ne &t &s t(e version policy t(at dec&des to ,(at e9tent t(ese %ules a%e en.o%ced. T(e /e%s&on pol&cy can be
spec&.&ed /&a t(e appl&cat&on con.&+u%at&on .&le.
0emem#er: /e%s&on&n+ &s only appl&ed to s(a%ed assembl&es> not p%&/ate assembl&es.
'. A""lication !omains
'.1 What is an A""lication !omain?
An App*oma&n can be t(ou+(t o. as a l&+(t,e&+(t p%ocess. Mult&ple App*oma&ns can e9&st &ns&de a '&n#! p%ocess. T(e
p%&ma%y pu%pose o. t(e App*oma&n &s to &solate an appl&cat&on .%om ot(e% appl&cat&ons.
'&n#! p%ocesses p%o/&de &solat&on by (a/&n+ d&st&nct memo%y add%ess spaces. T(&s &s e..ect&/e> but &t &s e9pens&/e and
doesn8t scale ,ell. T(e .NET %unt&me en.o%ces App*oma&n &solat&on by 2eep&n+ cont%ol o/e% t(e use o. memo%y all
memo%y &n t(e App*oma&n &s mana+ed by t(e .NET %unt&me> so t(e %unt&me can ensu%e t(at App*oma&ns do not access
eac( ot(e%8s memo%y.
'. 5ow does an A""!omain $et created?
App*oma&ns a%e usually c%eated by hosts. E9amples o. (osts a%e t(e '&ndo,s 7(ell> A7E.NET and $E. '(en you %un
a .NET appl&cat&on .%om t(e commandl&ne> t(e (ost &s t(e 7(ell. T(e 7(ell c%eates a ne, App*oma&n .o% e/e%y
appl&cat&on.
App*oma&ns can also be e9pl&c&tly c%eated by .NET appl&cat&ons. He%e &s a C# sample ,(&c( c%eates an App*oma&n>
c%eates an &nstance o. an ob;ect &ns&de &t> and t(en e9ecutes one o. t(e ob;ect8s met(ods. Note t(at you must name t(e
e9ecutable 8appdoma&ntest.e9e8 .o% t(&s code to ,o%2 as&s.
usin! System;
usin! System."untime."emotin!;
public class C#pp$omain%nfo &ars'al(y"ef)b*ect
{
public strin! +et#pp$omain%nfo()
{
return "#pp$omain , " - #pp$omain.Current$omain..rien/ly0ame;
}
}
public class #pp
{
public static int &ain()
{
#pp$omain a/ , #pp$omain.Create$omain( "#n/y1s ne2 /omain"3 null3
null );
)b*ectHan/le o' , a/.Create%nstance( "app/omaintest"3
"C#pp$omain%nfo" );
C#pp$omain%nfo a/%nfo , (C#pp$omain%nfo)(o'.4n2rap());
strin! info , a/%nfo.+et#pp$omain%nfo();
Console.WriteLine( "#pp$omain info " - info );
return 5;
}
}
'.& Can I write my own .NET host?
Qes. Fo% an e9ample o. (o, to do t(&s> ta2e a loo2 at t(e sou%ce .o% t(e dm.net mon&2e% de/eloped by Hason '(&tt&n+ton
and *on 5o9 @(ttp:CCsta...de/elop.comC;ason,Ccl%C%eadme.(tm A. T(e%e &s also a code sample &n t(e .NET 7*I called
Co%Host.
*. 7ar#a$e Collection
*.1 What is $ar#a$e collection?
:a%ba+e collect&on &s a system ,(e%eby a %unt&me component ta2es %espons&b&l&ty .o% mana+&n+ t(e l&.et&me o. ob;ects
and t(e (eap memo%y t(at t(ey occupy. T(&s concept &s not ne, to .NET Ha/a and many ot(e% lan+ua+esC%unt&mes (a/e
used +a%ba+e collect&on .o% some t&me.
*. Is it true that o#8ects don3t always $et destroyed immediately when the last re(erence $oes away?
Qes. T(e +a%ba+e collecto% o..e%s no +ua%antees about t(e t&me ,(en an ob;ect ,&ll be dest%oyed and &ts memo%y
%ecla&med.
T(e%e &s an &nte%est&n+ t(%ead &n t(e a%c(&/es> sta%ted by C(%&s 7ells> about t(e &mpl&cat&ons o. nondete%m&n&st&c
dest%uct&on o. ob;ects &n C#: (ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)A!L&nd"""3FLL*OTNETFEL6!-41B
$n Octobe% !"""> M&c%oso.t8s 5%&an Ha%%y posted a len+t(y analys&s o. t(e p%oblem:
(ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)A!L&nd""1"AFLL*OTNETFEL6!403!
C(%&s 7ells8 %esponse to 5%&an8s post&n+ &s (e%e: (ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)
A!L&nd""1"CFLL*OTNETFEL6B4#
*.& Why doesn3t the .NET runtime o((er deterministic destruction?
5ecause o. t(e +a%ba+e collect&on al+o%&t(m. T(e .NET +a%ba+e collecto% ,o%2s by pe%&od&cally %unn&n+ t(%ou+( a l&st o. all
t(e ob;ects t(at a%e cu%%ently be&n+ %e.e%enced by an appl&cat&on. All t(e ob;ects t(at &t doesn't .&nd du%&n+ t(&s sea%c( a%e
%eady to be dest%oyed and t(e memo%y %ecla&med. T(e &mpl&cat&on o. t(&s al+o%&t(m &s t(at t(e %unt&me doesn8t +et not&.&ed
&mmed&ately ,(en t(e .&nal %e.e%ence on an ob;ect +oes a,ay &t only .&nds out du%&n+ t(e ne9t s,eep o. t(e (eap.
Fut(e%mo%e> t(&s type o. al+o%&t(m ,o%2s best by pe%.o%m&n+ t(e +a%ba+e collect&on s,eep as %a%ely as poss&ble. No%mally
(eap e9(aust&on &s t(e t%&++e% .o% a collect&on s,eep.
*.' Is the lack o( deterministic destruction in .NET a "ro#lem?
$t8s ce%ta&nly an &ssue t(at a..ects component des&+n. $. you (a/e ob;ects t(at ma&nta&n e9pens&/e o% sca%ce %esou%ces @e.+.
database loc2sA> you need to p%o/&de some ,ay .o% t(e cl&ent to tell t(e ob;ect to %elease t(e %esou%ce ,(en &t &s done.
M&c%oso.t %ecommend t(at you p%o/&de a met(od called *&spose@A .o% t(&s pu%pose. Ho,e/e%> t(&s causes p%oblems .o%
d&st%&buted ob;ects &n a d&st%&buted system ,(o calls t(e *&spose@A met(od) 7ome .o%m o. %e.e%encecount&n+ o%
o,ne%s(&pmana+ement mec(an&sm &s needed to (andle d&st%&buted ob;ects un.o%tunately t(e %unt&me o..e%s no (elp ,&t(
t(&s.
*.* !oes non%deterministic destruction a((ect the usa$e o( C9: o#8ects (rom mana$ed code?
Qes. '(en us&n+ a COM ob;ect .%om mana+ed code> you a%e e..ect&/ely %ely&n+ on t(e +a%ba+e collecto% to call t(e .&nal
%elease on you% ob;ect. $. you% COM ob;ect (olds onto an e9pens&/e %esou%ce ,(&c( &s only cleanedup a.te% t(e .&nal
%elease> you may need to p%o/&de a ne, &nte%.ace on you% ob;ect ,(&c( suppo%ts an e9pl&c&t *&spose@A met(od.
*.+ I3)e heard that Finali;e methods should #e a)oided. 1hould I im"lement Finali;e on my class?
An ob;ect ,&t( a F&nal&<e met(od &s mo%e ,o%2 .o% t(e +a%ba+e collecto% t(an an ob;ect ,&t(out one. Also t(e%e a%e no
+ua%antees about t(e o%de% &n ,(&c( ob;ects a%e F&nal&<ed> so t(e%e a%e &ssues su%%ound&n+ access to ot(e% ob;ects .%om
t(e F&nal&<e met(od. F&nally> t(e%e &s no +ua%antee t(at a F&nal&<e met(od ,&ll +et called on an ob;ect> so &t s(ould ne/e% be
%el&ed upon to do cleanup o. an ob;ect8s %esou%ces.
M&c%oso.t %ecommend t(e .ollo,&n+ patte%n:
public class CTest %$isposable
{
public 6oi/ $ispose()
{
... // Cleanup acti6ities
+C.Suppress.inali7e(t'is);
}
8CTest() // C9 synta: 'i/in! t'e .inali7e() met'o/
{
$ispose();
}
}
$n t(e no%mal case t(e cl&ent calls *&spose@A> t(e ob;ect8s %esou%ces a%e .%eed> and t(e +a%ba+e collecto% &s %el&e/ed o. &ts
F&nal&<&n+ dut&es by t(e call to 7upp%essF&nal&<e@A. $n t(e ,o%st case> &.e. t(e cl&ent .o%+ets to call *&spose@A> t(e%e &s a
%easonable c(ance t(at t(e ob;ect8s %esou%ces ,&ll e/entually +et .%eed by t(e +a%ba+e collecto% call&n+ F&nal&<e@A. :&/en
t(e l&m&tat&ons o. t(e +a%ba+e collect&on al+o%&t(m t(&s seems l&2e a p%etty %easonable app%oac(.
*., !o I ha)e any control o)er the $ar#a$e collection al$orithm?
A l&ttle. Fo% e9ample> t(e 7ystem.:C class e9poses a Collect met(od t(&s .o%ces t(e +a%ba+e collecto% to collect all
un%e.e%enced ob;ects &mmed&ately.
*.- 5ow can I (ind out what the $ar#a$e collector is doin$?
Lots o. &nte%est&n+ stat&st&cs a%e e9po%ted .%om t(e .NET %unt&me /&a t(e 8.NET CL6 9998 pe%.o%mance counte%s. Jse
Ee%.o%mance Mon&to% to /&e, t(em.
+. 1eriali;ation
+.1 What is seriali;ation?
7e%&al&<at&on &s t(e p%ocess o. con/e%t&n+ an ob;ect &nto a st%eam o. bytes. *ese%&al&<at&on &s t(e oppos&te p%ocess o.
c%eat&n+ an ob;ect .%om a st%eam o. bytes. 7e%&al&<at&onC*ese%&al&<at&on &s mostly used to t%anspo%t ob;ects @e.+. du%&n+
%emot&n+A> o% to pe%s&st ob;ects @e.+. to a .&le o% databaseA.
+. !oes the .NET Framework ha)e in%#uilt su""ort (or seriali;ation?
T(e%e a%e t,o sepa%ate mec(an&sms p%o/&ded by t(e .NET class l&b%a%y =ml7e%&al&<e% and
7oapFo%matte%C5&na%yFo%matte%. M&c%oso.t uses =ml7e%&al&<e% .o% 'eb 7e%/&ces> and uses
7oapFo%matte%C5&na%yFo%matte% .o% %emot&n+. 5ot( a%e a/a&lable .o% use &n you% o,n code.
+.& I want to seriali;e instances o( my class. 1hould I use <ml1eriali;er= 1oa"Formatter or .inaryFormatter?
$t depends. =ml7e%&al&<e% (as se/e%e l&m&tat&ons suc( as t(e %eMu&%ement t(at t(e ta%+et class (as a pa%amete%less
const%ucto%> and only publ&c %eadC,%&te p%ope%t&es and .&elds can be se%&al&<ed. Ho,e/e%> on t(e plus s&de> =ml7e%&al&<e%
(as +ood suppo%t .o% custom&s&n+ t(e =ML document t(at &s p%oduced o% consumed. =ml7e%&al&<e%8s .eatu%es mean t(at &t
&s most su&table .o% c%ossplat.o%m ,o%2> o% .o% const%uct&n+ ob;ects .%om e9&st&n+ =ML documents.
7oapFo%matte% and 5&na%yFo%matte% (a/e .e,e% l&m&tat&ons t(an =ml7e%&al&<e%. T(ey can se%&al&<e p%&/ate .&elds> .o%
e9ample. Ho,e/e% t(ey bot( %eMu&%e t(at t(e ta%+et class be ma%2ed ,&t( t(e R7e%&al&<ableS att%&bute> so l&2e =ml7e%&al&<e%
t(e class needs to be ,%&tten ,&t( se%&al&<at&on &n m&nd. Also t(e%e a%e some Mu&%2s to ,atc( out .o% .o% e9ample on
dese%&al&<at&on t(e const%ucto% o. t(e ne, ob;ect &s not &n/o2ed.
T(e c(o&ce bet,een 7oapFo%matte% and 5&na%yFo%matte% depends on t(e appl&cat&on. 5&na%yFo%matte% ma2es sense
,(e%e bot( se%&al&<at&on and dese%&al&<at&on ,&ll be pe%.o%med on t(e .NET plat.o%m and ,(e%e pe%.o%mance &s &mpo%tant.
7oapFo%matte% +ene%ally ma2es mo%e sense &n all ot(e% cases> .o% ease o. debu++&n+ &. not(&n+ else.
+.' Can I customise the seriali;ation "rocess?
Qes. =ml7e%&al&<e% suppo%ts a %an+e o. att%&butes t(at can be used to con.&+u%e se%&al&<at&on .o% a pa%t&cula% class. Fo%
e9ample> a .&eld o% p%ope%ty can be ma%2ed ,&t( t(e R=ml$+no%eS att%&bute to e9clude &t .%om se%&al&<at&on. Anot(e% e9ample
&s t(e R=mlElementS att%&bute> ,(&c( can be used to spec&.y t(e =ML element name to be used .o% a pa%t&cula% p%ope%ty o%
.&eld.
7e%&al&<at&on /&a 7oapFo%matte%C5&na%yFo%matte% can also be cont%olled to some e9tent by att%&butes. Fo% e9ample> t(e
RNon7e%&al&<edS att%&bute &s t(e eMu&/alent o. =ml7e%&al&<e%8s R=ml$+no%eS att%&bute. Jlt&mate cont%ol o. t(e se%&al&<at&on
p%ocess can be ac(e&/ed by &mplement&n+ t(e t(e $7e%&al&<able &nte%.ace on t(e class ,(ose &nstances a%e to be se%&al&<ed.
+.* Why is <ml1eriali;er so slow?
T(e%e &s a oncepe%p%ocesspe%type o/e%(ead ,&t( =ml7e%&al&<e%. 7o t(e .&%st t&me you se%&al&<e o% dese%&al&<e an ob;ect
o. a +&/en type &n an appl&cat&on> t(e%e &s a s&+n&.&cant delay. T(&s no%mally doesn8t matte%> but &t may mean> .o% e9ample>
t(at =ml7e%&al&<e% &s a poo% c(o&ce .o% load&n+ con.&+u%at&on sett&n+s du%&n+ sta%tup o. a :J$ appl&cat&on.
+.+ Why do I $et errors when I try to seriali;e a 5ashta#le?
=ml7e%&al&<e% ,&ll %e.use to se%&al&<e &nstances o. any class t(at &mplements $*&ct&ona%y> e.+. Has(table. 7oapFo%matte%
and 5&na%yFo%matte% do not (a/e t(&s %est%&ct&on.
+., <ml1eriali;er is throwin$ a $eneric >There was an error re(lectin$ :yClass> error. 5ow do I (ind out what the
"ro#lem is?
Loo2 at t(e $nne%E9cept&on p%ope%ty o. t(e e9cept&on t(at &s t(%o,n to +et a mo%e spec&.&c e%%o% messa+e.
,. Attri#utes
,.1 What are attri#utes?
T(e%e a%e at least t,o types o. .NET att%&bute. T(e .&%st type $ ,&ll %e.e% to as a metadata att%&bute &t allo,s some data to
be attac(ed to a class o% met(od. T(&s data becomes pa%t o. t(e metadata .o% t(e class> and @l&2e ot(e% class metadataA
can be accessed /&a %e.lect&on. An e9ample o. a metadata att%&bute &s Rse%&al&<ableS> ,(&c( can be attac(ed to a class and
means t(at &nstances o. t(e class can be se%&al&<ed.
;seriali7able< public class CTest {}
T(e ot(e% type o. att%&bute &s a conte9t att%&bute. Conte9t att%&butes use a s&m&la% synta9 to metadata att%&butes but t(ey a%e
.undamentally d&..e%ent. Conte9t att%&butes p%o/&de an &nte%cept&on mec(an&sm ,(e%eby &nstance act&/at&on and met(od
calls can be p%e andCo% postp%ocessed. $. you8/e come ac%oss Ie&t( 5%o,n8s un&/e%sal dele+ato% you8ll be .am&l&a% ,&t(
t(&s &dea.
,. Can I create my own metadata attri#utes?
Qes. 7&mply de%&/e a class .%om 7ystem.Att%&bute and ma%2 &t ,&t( t(e Att%&buteJsa+e att%&bute. Fo% e9ample:
;#ttribute4sa!e(#ttributeTar!ets.Class)<
public class %nspire/(y#ttribute System.#ttribute
{
public strin! %nspire/(y;
public %nspire/(y#ttribute( strin! inspire/(y )
{
%nspire/(y , inspire/(y;
}
}
;%nspire/(y("#n/y &c1s brilliant .0=T .#>")<
class CTest
{
}
class C#pp
{
public static 6oi/ &ain()
{
ob*ect;< atts , typeof(CTest).+etCustom#ttributes(true);
foreac'( ob*ect att in atts )
if( att is %nspire/(y#ttribute )
Console.WriteLine( "Class CTest 2as inspire/ by
{5}"3 ((%nspire/(y#ttribute)att).%nspire/(y );
}
}
,.& Can I create my own conte4t attri#utes?
Qes. Ta2e a loo2 at *on 5o98s sample @called CallT(%es(oldA at (ttp:CC,,,.de/elop.comCdbo9CdotnetCt(%es(oldC> and also
Eete% *%ayton8s T%ace(oo2.NET at (ttp:CC,,,.%a<o%so.t.netC
-. Code Access 1ecurity
-.1 What is Code Access 1ecurity ?CA1@?
CA7 &s t(e pa%t o. t(e .NET secu%&ty model t(at dete%m&nes ,(et(e% o% not a p&ece o. code &s allo,ed to %un> and ,(at
%esou%ces &t can use ,(en &t &s %unn&n+. Fo% e9ample> &t &s CA7 t(at ,&ll p%e/ent a .NET ,eb applet .%om .o%matt&n+ you%
(a%d d&s2.
-. 5ow does CA1 work?
T(e CA7 secu%&ty pol&cy %e/ol/es a%ound t,o 2ey concepts code +%oups and pe%m&ss&ons. Eac( .NET assembly &s a
membe% o. a pa%t&cula% code $rou"> and eac( code +%oup &s +%anted t(e pe%m&ss&ons spec&.&ed &n a named "ermission
set.
Fo% e9ample> us&n+ t(e de.ault secu%&ty pol&cy> a cont%ol do,nloaded .%om a ,eb s&te belon+s to t(e 8Tone $nte%net8 code
+%oup> ,(&c( ad(e%es to t(e pe%m&ss&ons de.&ned by t(e 8$nte%net8 named pe%m&ss&on set. @Natu%ally t(e 8$nte%net8 named
pe%m&ss&on set %ep%esents a /e%y %est%&ct&/e %an+e o. pe%m&ss&ons.A
-.& Who de(ines the CA1 code $rou"s?
M&c%oso.t de.&nes some de.ault ones> but you can mod&.y t(ese and e/en c%eate you% o,n. To see t(e code +%oups de.&ned
on you% system> %un 8caspol l+8 .%om t(e commandl&ne. On my system &t loo2s l&2e t(&s:
Le6el , &ac'ine
Co/e +roups
?. #ll co/e 0ot'in!
?.?. @one A &yComputer .ullTrust
?.?.?. Honor SBipCerification reDuests SBipCerification
?.E. @one A %ntranet Local%ntranet
?.F. @one A %nternet %nternet
?.G. @one A 4ntruste/ 0ot'in!
?.H. @one A Truste/ %nternet
?.I. Stron!0ame A 55EG55555GJ55555KG5555555I5E555555EG5555HEHFG?F?555G55555F
555555C.C(FEK?##L?H.=KK$G5$GK5G5FFI.K5HI$LJJI.=$GILLH(CL((HGF5(#GGGG.=.JFGJ=($5I
.KIE.FKLLI#=G$CF(L(5G#L.=I.GK.EH.LG5GEF=(.EC5(JKIKJ$J$5J#CGJ$IKC=$5.CJ.JF(GIH=5J
5L#C??=C?$CCL$5HG=J5L#GFFFI$$=G5J#HFKF#GJHHI?EFELEC====LE.?II5(L?KEL$FJHI?##(.HC
#C?$.?LFGIFFCI5E.J.E$H =6eryt'in!
Note t(e (&e%a%c(y o. code +%oups t(e top o. t(e (&e%a%c(y &s t(e most +ene%al @8All code8A> ,(&c( &s t(en subd&/&ded &nto
se/e%al +%oups> eac( o. ,(&c( &n tu%n can be subd&/&ded. Also note t(at @some,(at counte%&ntu&t&/elyA a sub+%oup can be
assoc&ated ,&t( a mo%e pe%m&ss&/e pe%m&ss&on set t(an &ts pa%ent.
-.' 5ow do I de(ine my own code $rou"?
Jse caspol. Fo% e9ample> suppose you t%ust code .%om ,,,.mydoma&n.com and you ,ant &t (a/e .ull access to you%
system> but you ,ant to 2eep t(e de.ault %est%&ct&ons .o% all ot(e% &nte%net s&tes. To ac(&e/e t(&s> you ,ould add a ne, code
+%oup as a sub+%oup o. t(e 8Tone $nte%net8 +%oup> l&2e t(&s:
caspol Aa! ?.F Asite 222.my/omain.com .ullTrust
No, &. you %un caspol l+ you ,&ll see t(at t(e ne, +%oup (as been added as +%oup 1.#.1:
...
?.F. @one A %nternet %nternet
?.F.?. Site A 222.my/omain.com .ullTrust
...
Note t(at t(e nume%&c label @1.#.1A &s ;ust a caspol &n/ent&on to ma2e t(e code +%oups easy to man&pulate .%om t(e
commandl&ne. T(e unde%ly&n+ %unt&me ne/e% sees &t.
-.* 5ow do I chan$e the "ermission set (or a code $rou"?
Jse caspol. $. you a%e t(e mac(&ne adm&n&st%ato%> you can ope%ate at t(e 8mac(&ne8 le/el ,(&c( means not only t(at t(e
c(an+es you ma2e become t(e de.ault .o% t(e mac(&ne> but also t(at use%s cannot c(an+e t(e pe%m&ss&ons to be mo%e
pe%m&ss&/e. $. you a%e a no%mal @nonadm&nA use% you can st&ll mod&.y t(e pe%m&ss&ons> but only to ma2e t(em mo%e
%est%&ct&/e. Fo% e9ample> to allo, &nt%anet code to do ,(at &t l&2es you m&+(t do t(&s:
caspol Ac! ?.E .ullTrust
Note t(at because t(&s &s mo%e pe%m&ss&/e t(an t(e de.ault pol&cy @on a standa%d systemA> you s(ould only do t(&s at t(e
mac(&ne le/el do&n+ &t at t(e use% le/el ,&ll (a/e no e..ect.
-.+ Can I create my own "ermission set?
Qes. Jse caspol ap> spec&.y&n+ an =ML .&le conta&n&n+ t(e pe%m&ss&ons &n t(e pe%m&ss&on set. To sa/e you some t&me>
(e%e &s a sample .&le co%%espond&n+ to t(e 8E/e%yt(&n+8 pe%m&ss&on set ;ust ed&t to su&t you% needs. '(en you (a/e ed&ted
t(e sample> add &t to t(e %an+e o. a/a&lable pe%m&ss&on sets l&2e t(&s:
caspol Aap samplepermset.:ml
T(en> to apply t(e pe%m&ss&on set to a code +%oup> do somet(&n+ l&2e t(&s:
caspol Ac! ?.F SampleMermSet
@5y de.ault> 1.# &s t(e 8$nte%net8 code +%oupA
-., I3m ha)in$ some trou#le with CA1. 5ow can I dia$nose my "ro#lem?
Caspol (as a couple o. opt&ons t(at m&+(t (elp. F&%st> you can as2 caspol to tell you ,(at code +%oup an assembly belon+s
to> us&n+ caspol %s+. 7&m&la%ly> you can as2 ,(at pe%m&ss&ons a%e be&n+ appl&ed to a pa%t&cula% assembly us&n+ caspol %sp.
-.- I can3t #e #othered with all this CA1 stu((. Can I turn it o((?
Qes> as lon+ as you a%e an adm&n&st%ato%. Hust %un:
caspol As off
A. Intermediate /an$ua$e ?I/@
A.1 Can I look at the I/ (or an assem#ly?
Qes. M7 supply a tool called $ldasm ,(&c( can be used to /&e, t(e metadata and $L .o% an assembly.
A. Can source code #e re)erse%en$ineered (rom I/?
Qes> &t &s o.ten %elat&/ely st%a&+(t.o%,a%d to %e+ene%ate (&+(le/el sou%ce @e.+. C#A .%om $L.
A.& 5ow can I sto" my code #ein$ re)erse%en$ineered (rom I/?
T(e%e &s cu%%ently no s&mple ,ay to stop code be&n+ %e/e%seen+&nee%ed .%om $L. $n .utu%e &t &s l&2ely t(at $L ob.uscat&on
tools ,&ll become a/a&lable> e&t(e% .%om M7 o% .%om t(&%d pa%t&es. T(ese tools ,o%2 by 8opt&m&s&n+8 t(e $L &n suc( a ,ay t(at
%e/e%seen+&nee%&n+ becomes muc( mo%e d&..&cult.
O. cou%se &. you a%e ,%&t&n+ ,eb se%/&ces t(en %e/e%seen+&nee%&n+ &s not a p%oblem as cl&ents do not (a/e access to you%
$L.
A.' Can I write I/ "ro$rams directly?
Qes. Eete% *%ayton posted t(&s s&mple e9ample to t(e *OTNET ma&l&n+ l&st:
.assembly &y#ssembly {}
.class &y#pp {
.met'o/ static 6oi/ &ain() {
.entrypoint
l/str "Hello3 %LN"
call 6oi/ System.ConsoleWriteLine(class System.)b*ect)
ret
}
}
Hust put t(&s &nto a .&le called (ello.&l> and t(en %un &lasm (ello.&l. An e9e assembly ,&ll be +ene%ated.
A.* Can I do thin$s in I/ that I can3t do in C2?
Qes. A couple o. s&mple e9amples a%e t(at you can t(%o, e9cept&ons t(at a%e not de%&/ed .%om 7ystem.E9cept&on> and you
can (a/e non<e%obased a%%ays.
1B. Im"lications (or C9:
1B.1 Is C9: dead?
T(&s sub;ect causes a lot o. cont%o/e%sy> as you8ll see &. you %ead t(e ma&l&n+ l&st a%c(&/es. Ta2e a loo2 at t(e .ollo,&n+ t,o
t(%eads:
(ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)A!L&nd"""3FLL*OTNETF*L"FEL14!-1
(ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)A!L&nd"""3FLL*OTNETFEL61"311
F'$' my /&e, &s as .ollo,s: COM &s many t(&n+s> and &t8s d&..e%ent t(&n+s to d&..e%ent people. 5ut to me> COM &s
.undamentally about (o, l&ttle blobs o. code .&nd ot(e% l&ttle blobs o. code> and (o, t(ey commun&cate ,&t( eac( ot(e%
,(en t(ey .&nd eac( ot(e%. COM spec&.&es p%ec&sely (o, t(&s locat&on and commun&cat&on ta2es place. $n a 8pu%e8 .NET
,o%ld> cons&st&n+ ent&%ely o. .NET ob;ects> l&ttle blobs o. code st&ll .&nd eac( ot(e% and tal2 to eac( ot(e%> but t(ey don8t use
COM to do so. T(ey use a model ,(&c( &s s&m&la% to COM &n some ,ays .o% e9ample> type &n.o%mat&on &s sto%ed &n a
tabula% .o%m pac2a+ed ,&t( t(e component> ,(&c( &s Mu&te s&m&la% to pac2a+&n+ a type l&b%a%y ,&t( a COM component. 5ut
&t8s not COM.
7o> does t(&s matte%) 'ell> $ don8t %eally ca%e about most o. t(e COM stu.. +o&n+ a,ay $ don8t ca%e t(at .&nd&n+
components doesn8t &n/ol/e a t%&p to t(e %e+&st%y> o% t(at $ don8t use $*L to de.&ne my &nte%.aces. 5ut t(e%e &s one t(&n+ t(at $
,ouldn8t l&2e to +o a,ay $ ,ouldn8t l&2e to lose t(e &dea o. &nte%.acebased de/elopment. COM8s +%eatest st%en+t(> &n my
op&n&on> &s &ts &ns&stence on a cast&%on sepa%at&on bet,een &nte%.ace and &mplementat&on. Jn.o%tunately> t(e .NET
.%ame,o%2 seems to ma2e no suc( &ns&stence &t lets you do &nte%.acebased de/elopment> but &t doesn8t &ns&st. 7ome
people ,ould a%+ue t(at (a/&n+ a c(o&ce can ne/e% be a bad t(&n+> and maybe t(ey8%e %&+(t> but $ can8t (elp .eel&n+ t(at
maybe &t8s a bac2,a%d step.
1B. Is !C9: dead?
E%etty muc(> .o% .NET de/elope%s. T(e .NET F%ame,o%2 (as a ne, %emot&n+ model ,(&c( &s not based on *COM. O.
cou%se *COM ,&ll st&ll be used &n &nte%op scena%&os.
1B.& Is :T1CC9:D dead?
No. T(e app%oac( .o% t(e .&%st .NET %elease &s to p%o/&de access to t(e e9&st&n+ COMD se%/&ces @t(%ou+( an &nte%op laye%A
%at(e% t(an %eplace t(e se%/&ces ,&t( nat&/e .NET ones. Ga%&ous tools and att%&butes a%e p%o/&ded to t%y to ma2e t(&s as
pa&nless as poss&ble. T(e E*C %elease o. t(e .NET 7*I &ncludes &nte%op suppo%t .o% co%e se%/&ces @H$T act&/at&on>
t%ansact&onsA but not some o. t(e (&+(e% le/el se%/&ces @e.+. COMD E/ents> Queued componentsA.
O/e% t&me &t &s e9pected t(at &nte%op ,&ll become mo%e seamless t(&s may mean t(at some se%/&ces become a co%e pa%t
o. t(e CL6> andCo% &t may mean t(at some se%/&ces ,&ll be %e,%&tten as mana+ed code ,(&c( %uns on top o. t(e CL6.
Fo% mo%e on t(&s top&c> sea%c( .o% post&n+s by Hoe Lon+ &n t(e a%c(&/es Hoe &s t(e M7 +%oup mana+e% .o% COMD. 7ta%t
,&t( t(&s messa+e:
(ttp:CCd&scuss.de/elop.comCa%c(&/esC,a.e9e)A!L&nd"""3FLL*OTNETFEL614#3"
1B.' Can I use C9: com"onents (rom .NET "ro$rams?
Qes. COM components a%e accessed .%om t(e .NET %unt&me /&a a 6unt&me Callable '%appe% @6C'A. T(&s ,%appe% tu%ns
t(e COM &nte%.aces e9posed by t(e COM component &nto .NETcompat&ble &nte%.aces. Fo% oleautomat&on &nte%.aces> t(e
6C' can be +ene%ated automat&cally .%om a type l&b%a%y. Fo% nonoleautomat&on &nte%.aces> &t may be necessa%y to
de/elop a custom 6C' ,(&c( manually maps t(e types e9posed by t(e COM &nte%.ace to .NETcompat&ble types.
He%e8s a s&mple e9ample .o% t(ose .am&l&a% ,&t( ATL. F&%st> c%eate an ATL component ,(&c( &mplements t(e .ollo,&n+ $*L:
import "oai/l.i/l";
import "oci/l.i/l";

;
ob*ect3
uui/(=#5?F.KFAGJL#AGG5FAJI=CA.$K.==H=IE5I)3
'elpstrin!("%Cpp0ame %nterface")3
pointerO/efault(uniDue)3
oleautomation
<
interface %Cpp0ame %4nBno2n
{
;'elpstrin!("met'o/ Set0ame")< H"=S4LT Set0ame(;in< (ST" name);
;'elpstrin!("met'o/ +et0ame")< H"=S4LT +et0ame(;out3ret6al< (ST" Pp0ame );
};
;
uui/(.H=GCI?$A$KF#AGEKHA#G(GAEGHF$G#GGJG$)3
6ersion(?.5)3
'elpstrin!("cppcomser6er ?.5 Type Library")
<
library CMMC)&S="C="Lib
{
importlib("st/oleFE.tlb");
importlib("st/oleE.tlb");
;
uui/(I55C=I$KAH=$LAG(G$A((GKA=J$H$H5KI.L5)3
'elpstrin!("Cpp0ame Class")
<
coclass Cpp0ame
{
;/efault< interface %Cpp0ame;
};
};
'(en you8/e bu&lt t(e component> you s(ould +et a typel&b%a%y. 6un t(e TL5$ME ut&l&ty on t(e typel&ba%y> l&2e t(&s:
tlbimp cppcomser6er.tlb
$. success.ul> you ,&ll +et a messa+e l&2e t(&s:
Typelib importe/ successfully to CMMC)&S="C="Lib./ll
Qou no, need a .NET cl&ent let8s use C#. C%eate a .cs .&le conta&n&n+ t(e .ollo,&n+ code:
usin! System;
usin! CMMC)&S="C="Lib;
public class &ain#pp
{
static public 6oi/ &ain()
{
Cpp0ame cppname , ne2 Cpp0ame();
cppname.Set0ame( "bob" );
Console.WriteLine( "0ame is " - cppname.+et0ame() );
}
}
Note t(at ,e a%e us&n+ t(e type l&b%a%y name as a namespace> and t(e COM class name as t(e class. Alte%nat&/ely ,e
could (a/e used CEECOM7E6GE6L&b.CppName .o% t(e class name and +one ,&t(out t(e us&n+ CEECOM7E6GE6L&b
statement.
Comp&le t(e C# code l&2e t(&s:
csc /rcppcomser6erlib./ll cs'arpcomclient.cs
Note t(at t(e comp&le% &s be&n+ told to %e.e%ence t(e *LL ,e p%e/&ously +ene%ated .%om t(e typel&b%a%y us&n+ TL5$ME.
Qou s(ould no, be able to %un cs(a%pcomcl&ent.e9e> and +et t(e .ollo,&n+ output on t(e console:
0ame is bob
1B.* Can I use .NET com"onents (rom C9: "ro$rams?
Qes. .NET components a%e accessed .%om COM /&a a COM Callable '%appe% @CC'A. T(&s &s s&m&la% to a 6C' @see
p%e/&ous Muest&onA> but ,o%2s &n t(e oppos&te d&%ect&on. A+a&n> &. t(e ,%appe% cannot be automat&cally +ene%ated by t(e
.NET de/elopment tools> o% &. t(e automat&c be(a/&ou% &s not des&%able> a custom CC' can be de/eloped. Also> .o% COM
to 8see8 t(e .NET component> t(e .NET component must be %e+&ste%ed &n t(e %e+&st%y.
He%e8s a s&mple e9ample. C%eate a C# .&le called testcomse%/e%.cs and put t(e .ollo,&n+ &n &t:

usin! System;
namespace #n/y&c
{
;Class%nterface(Class%nterfaceType.#uto$ual)<
public class CS'arpC)&Ser6er
{
public CS'arpC)&Ser6er() {}
public 6oi/ Set0ame( strin! name ) { mOname , name; }
public strin! +et0ame() { return mOname; }
pri6ate strin! mOname;
}
}
T(en comp&le t(e .cs .&le as .ollo,s:
csc /tar!etlibrary testcomser6er.cs
Qou s(ould +et a dll> ,(&c( you %e+&ste% l&2e t(&s:
re!asm testcomser6er./ll /tlbtestcomser6er.tlb /co/ebase
No, you need to c%eate a cl&ent to test you% .NET COM component. G57c%&pt ,&ll do put t(e .ollo,&n+ &n a .&le called
comcl&ent./bs:
$im /ot0et)b*
Set /ot0et)b* , Create)b*ect("#n/y&c.CS'arpC)&Ser6er")
/ot0et)b*.Set0ame ("bob")
&s!(o: "0ame is " Q /ot0et)b*.+et0ame()
and %un t(e sc%&pt l&2e t(&s:
2script comclient.6bs
And (ey p%esto you s(ould +et a messa+e bo9 d&splayed ,&t( t(e te9t ?Name &s bob?.
An alte%nat&/e to t(e app%oac( abo/e &t to use t(e dm.net mon&2e% de/eloped by Hason '(&tt&n+ton and *on 5o9. :o to
(ttp:CCsta...de/elop.comC;ason,Ccl%C%eadme.(tm to c(ec2 &t out.
1B.+ Is AT/ redundant in the .NET world?
Qes> &. you a%e ,%&t&n+ appl&cat&ons t(at l&/e &ns&de t(e .NET .%ame,o%2. O. cou%se many de/elope%s may ,&s( to cont&nue
us&n+ ATL to ,%&te CDD COM components t(at l&/e outs&de t(e .%ame,o%2> but &. you a%e &ns&de you ,&ll almost ce%ta&nly
,ant to use C#. 6a, CDD @and t(e%e.o%e ATL ,(&c( &s based on &tA doesn8t (a/e muc( o. a place &n t(e .NET ,o%ld &t8s
;ust too nea% t(e metal and p%o/&des too muc( .le9&b&l&ty .o% t(e %unt&me to be able to mana+e &t.
11. :iscellaneous
11.1 5ow does .NET remotin$ work?
.NET %emot&n+ &n/ol/es send&n+ messa+es alon+ c(annels. T,o o. t(e standa%d c(annels a%e HTTE and TCE. TCE &s
&ntended .o% LANs only HTTE can be used .o% LANs o% 'ANs @&nte%netA.
7uppo%t &s p%o/&ded .o% mult&ple messa+e se%&al&<a%&on .o%mats. E9amples a%e 7OAE @=MLbasedA and b&na%y. 5y de.ault>
t(e HTTE c(annel uses 7OAE @/&a t(e .NET %unt&me 7e%&al&<at&on 7OAE Fo%matte%A> and t(e TCE c(annel uses b&na%y
@/&a t(e .NET %unt&me 7e%&al&<at&on 5&na%y Fo%matte%A. 5ut e&t(e% c(annel can use e&t(e% se%&al&<at&on .o%mat.
T(e%e a%e a numbe% o. styles o. %emote access:
SingleCall. Eac( &ncom&n+ %eMuest .%om a cl&ent &s se%/&ced by a ne, ob;ect. T(e ob;ect &s t(%o,n a,ay ,(en t(e
%eMuest (as .&n&s(ed. T(&s @essent&ally statelessA model can be made state.ul &n t(e A7E.NET en/&%onment by us&n+
t(e A7E.NET state se%/&ce to sto%e appl&cat&on o% sess&on state.

Singleton. All &ncom&n+ %eMuests .%om cl&ents a%e p%ocessed by a s&n+le se%/e% ob;ect.

Client-activated object. T(&s &s t(e old state.ul @*ACOM model ,(e%eby t(e cl&ent %ece&/es a %e.e%ence to t(e %emote
ob;ect and (olds t(at %e.e%ence @t(us 2eep&n+ t(e %emote ob;ect al&/eA unt&l &t &s .&n&s(ed ,&t( &t.
*&st%&buted +a%ba+e collect&on o. ob;ects &s mana+ed by a system called 8leased based l&.et&me8. Eac( ob;ect (as a lease
t&me> and ,(en t(at t&me e9p&%es t(e ob;ect &s d&sconnected .%om t(e .NET %unt&me %emot&n+ &n.%ast%uctu%e. Ob;ects (a/e a
de.ault %ene, t&me t(e lease &s %ene,ed ,(en a success.ul call &s made .%om t(e cl&ent to t(e ob;ect. T(e cl&ent can also
e9pl&c&tly %ene, t(e lease.
$. you8%e &nte%ested &n us&n+ =ML6EC as an alte%nat&/e to 7OAE> ta2e a loo2 at C(a%les Coo28s =ML6EC.Net s&te at
(ttp:CC,,,.coo2comput&n+.comC9ml%pcC9ml%pc.s(tml.
11. 5ow can I $et at the Win& AEI (rom a .NET "ro$ram?
Jse EC$n/o2e. T(&s uses s&m&la% tec(nolo+y to COM $nte%op> but &s used to access stat&c *LL ent%y po&nts &nstead o. COM
ob;ects. He%e &s an e9ample o. C# call&n+ t(e '&n#! Messa+e5o9 .unct&on:
usin! System;
usin! System."untime.%nteropSer6ices;
class &ain#pp
{
;$ll%mport("userFE./ll"3 =ntryMoint,"&essa!e(o:"3 SetLast=rror,true3
C'arSet,C'arSet.#uto)<
public static e:tern int &essa!e(o:(int 'Wn/3 Strin! str&essa!e3 Strin!
strCaption3 uint uiType);
public static 6oi/ &ain()
{
&essa!e(o:( 53 "Hello3 t'is is M%n6oBe in operationN"3 ".0=T"3 5 );
}
}
1. Class /i#rary
1.1 File IC9
1.1.1 5ow do I read (rom a te4t (ile?
F&%st> use a 7ystem.$O.F&le7t%eam ob;ect to open t(e .&le:
.ileStream fs , ne2 .ileStream( R"cStest.t:t"3 .ile&o/e.)pen3 .ile#ccess."ea/ );
F&le7t%eam &n(e%&ts .%om 7t%eam> so you can ,%ap t(e F&le7t%eam ob;ect ,&t( a 7t%eam6eade% ob;ect. T(&s p%o/&des a n&ce
&nte%.ace .o% p%ocess&n+ t(e st%eam l&ne by l&ne:
Stream"ea/er sr , ne2 Stream"ea/er( fs );
strin! curLine;
2'ile( (curLine , sr."ea/Line()) N, null )
Console.WriteLine( curLine );
F&nally close t(e 7t%eam6eade% ob;ect:
sr.Close();
Note t(at t(&s ,&ll automat&cally call Close@A on t(e unde%ly&n+ 7t%eam ob;ect> so an e9pl&c&t .s.Close@A &s not %eMu&%ed.
1.1. 5ow do I write to a te4t (ile?
7&m&la% to t(e %ead e9ample> e9cept use 7t%eam'%&te% &nstead o. 7t%eam6eade%.
1.1.& 5ow do I readCwrite #inary (iles?
7&m&la% to te9t .&les> e9cept ,%ap t(e F&le7t%eam ob;ect ,&t( a 5&na%y6eade%C'%&te% ob;ect &nstead o. a 7t%eam6eade%C'%&te%
ob;ect.
1.1.' 5ow do I delete a (ile?
Jse t(e stat&c *elete@A met(od on t(e 7ystem.$O.F&le ob;ect:
.ile.$elete( R"cStest.t:t" );
1. Te4t Erocessin$
1..1 Are re$ular e4"ressions su""orted?
Qes. Jse t(e 7ystem.Te9t.6e+ula%E9p%ess&ons.6e+e9 class. Fo% e9ample> t(e .ollo,&n+ code updates t(e t&tle &n an HTML
.&le:
.ileStream fs , ne2 .ileStream( "test.'tm"3 .ile&o/e.)pen3 .ile#ccess."ea/ );
Stream"ea/er sr , ne2 Stream"ea/er( fs );
"e!e: r , ne2 "e!e:( "TT%TL=U(.P)T/T%TL=U" );
strin! s;
2'ile( (s , sr."ea/Line()) N, null )
{
if( r.%s&atc'( s ) )
s , r."eplace( s3 "TT%TL=U0e2 an/ impro6e/ V{?}T/T%TL=U" );
Console.WriteLine( s );
}
1.& Internet
1.&.1 5ow do I download a we# "a$e?
F&%st use t(e 7ystem.Net.'eb6eMuestFacto%y class to acMu&%e a 'eb6eMuest ob;ect:
Web"eDuest reDuest , Web"eDuest.Create( "'ttp//local'ost" );
T(en as2 .o% t(e %esponse .%om t(e %eMuest:
Web"esponse response , reDuest.+et"esponse();
T(e :et6esponse met(od bloc2s unt&l t(e do,nload &s complete. T(en you can access t(e %esponse st%eam l&2e t(&s:
Stream s , response.+et"esponseStream();
// )utput t'e /o2nloa/e/ stream to t'e console
Stream"ea/er sr , ne2 Stream"ea/er( s );
strin! line;
2'ile( (line , sr."ea/Line()) N, null )
Console.WriteLine( line );
Note t(at 'eb6eMuest and 'eb6eponse ob;ects can be do,ncast to Http'eb6eMuest and Http'eb6eponse ob;ects
%espect&/ely> to access (ttpspec&.&c .unct&onal&ty.
1.&. 5ow do I use a "ro4y?
T,o app%oac(es to a..ect all ,eb %eMuests do t(&s:
System.0et.+lobalMro:ySelection.Select , ne2 WebMro:y( "pro:yname"3 J5 );
Alte%nat&/ely> to set t(e p%o9y .o% a spec&.&c ,eb %eMuest> do t(&s:
HttpWeb"eDuest reDuest , (HttpWeb"eDuest)Web"eDuest.Create( "'ttp//local'ost" );
reDuest.Mro:y , ne2 WebMro:y( "pro:yname"3 J5 );
1.' <:/
1.'.1 Is !9: su""orted?
Qes. Ta2e t(&s e9ample =ML document:
TM=)ML=U
TM="S)0U.re/T/M="S)0U
TM="S)0U(illT/M="S)0U
T/M=)ML=U
T(&s document can be pa%sed as .ollo,s:
Wml$ocument /oc , ne2 Wml$ocument();
/oc.Loa/( "test.:ml" );
Wml0o/e root , /oc.$ocument=lement;
foreac'( Wml0o/e person=lement in root.C'il/0o/es )
Console.WriteLine( person=lement..irstC'il/.Calue.ToStrin!() );
T(e output &s:
.re/
(ill
1.'. Is 1A< su""orted?
No. $nstead> a ne, =ml6eade%C=ml'%&te% AE$ &s o..e%ed. L&2e 7A= &t &s st%eambased but &t uses a 8pull8 model %at(e% t(an
7A=8s 8pus(8 model. He%e8s an e9ample:
WmlTe:t"ea/er rea/er , ne2 WmlTe:t"ea/er( "test.:ml" );
2'ile( rea/er."ea/() )
{
if( rea/er.0o/eType ,, Wml0o/eType.=lement QQ rea/er.0ame ,, "M="S)0" )
{
rea/er."ea/(); // SBip to t'e c'il/ te:t
Console.WriteLine( rea/er.Calue );
}
}
1.'.& Is <Eath su""orted?
Qes> /&a t(e =Eat(=== classes:
WMat'$ocument :p/oc , ne2 WMat'$ocument("test.:ml");
WMat'0a6i!ator na6 , :p/oc.Create0a6i!ator();
WMat'=:pression e:pr , na6.Compile("/escen/antM=)ML=/M="S)0");

WMat'0o/e%terator iterator , na6.Select(e:pr);
2'ile (iterator.&o6e0e:t())
Console.WriteLine(iterator.Current);
1.* Threadin$
1.*.1 Is multi%threadin$ su""orted?
Qes> t(e%e &s e9tens&/e suppo%t .o% mult&t(%ead&n+. Ne, t(%eads can be spa,ned> and t(e%e &s a systemp%o/&ded
t(%eadpool ,(&c( appl&cat&ons can use.
1.*. 5ow do I s"awn a thread?
C%eate an &nstance o. a 7ystem.T(%ead&n+.T(%ead ob;ect> pass&n+ &t an &nstance o. a T(%ead7ta%t dele+ate t(at ,&ll be
e9ecuted on t(e ne, t(%ead. Fo% e9ample:
class &yT'rea/
{
public &yT'rea/( strin! init$ata )
{
mO/ata , init$ata;
mOt'rea/ , ne2 T'rea/( ne2 T'rea/Start(T'rea/&ain) );
mOt'rea/.Start();
}
// T'rea/&ain() is e:ecute/ on t'e ne2 t'rea/.
pri6ate 6oi/ T'rea/&ain()
{
Console.WriteLine( mO/ata );
}
public 6oi/ Wait4ntil.inis'e/()
{
mOt'rea/.Xoin();
}
pri6ate T'rea/ mOt'rea/;
pri6ate strin! mO/ata;
}
$n t(&s case c%eat&n+ an &nstance o. t(e MyT(%ead class &s su..&c&ent to spa,n t(e t(%ead and e9ecute t(e
MyT(%ead.T(%eadMa&n@A met(od:
&yT'rea/ t , ne2 &yT'rea/( "Hello3 2orl/." );
t.Wait4ntil.inis'e/();
1.*.& 5ow do I sto" a thread?
T(e%e a%e se/e%al opt&ons. F&%st> you can use you% o,n commun&cat&on mec(an&sm to tell t(e T(%ead7ta%t met(od to .&n&s(.
Alte%nat&/ely t(e T(%ead class (as &nbu&lt suppo%t .o% &nst%uct&n+ t(e t(%ead to stop. T(e t,o p%&nc&ple met(ods a%e
T(%ead.$nte%%upt@A and T(%ead.Abo%t@A. T(e .o%me% ,&ll cause a T(%ead$nte%%uptedE9cept&on to be t(%o,n on t(e t(%ead
,(en &t ne9t +oes &nto a 'a&tHo&n7leep state. $n ot(e% ,o%ds> T(%ead.$nte%%upt &s a pol&te ,ay o. as2&n+ t(e t(%ead to stop
,(en &t &s no lon+e% do&n+ any use.ul ,o%2. $n cont%ast> T(%ead.Abo%t@A t(%o,s a T(%eadAbo%tE9cept&on %e+a%dless o. ,(at
t(e t(%ead &s do&n+. Fu%t(e%mo%e> t(e T(%eadAbo%tE9cept&on cannot no%mally be cau+(t @t(ou+( t(e T(%ead7ta%t8s .&nally
met(od will be e9ecutedA. T(%ead.Abo%t@A &s a (ea/y(anded mec(an&sm ,(&c( s(ould not no%mally be %eMu&%ed.
1.*.' 5ow do I use the thread "ool?
5y pass&n+ an &nstance o. a 'a&tCallbac2 dele+ate to t(e T(%eadEool.QueueJse%'o%2$tem@A met(od:
class C#pp
{
static 6oi/ &ain()
{
strin! s , "Hello3 Worl/";
T'rea/Mool.>ueue4serWorB%tem( ne2 WaitCallbacB( $oWorB )3 s );
T'rea/.Sleep( ?555 ); // +i6e time for 2orB item to be e:ecute/
}
// $oWorB is e:ecute/ on a t'rea/ from t'e t'rea/ pool.
static 6oi/ $oWorB( ob*ect state )
{
Console.WriteLine( state );
}
}
1.*.* 5ow do I know when my thread "ool work item has com"leted?
T(e%e &s no ,ay to Mue%y t(e t(%ead pool .o% t(&s &n.o%mat&on. Qou must put code &nto t(e 'a&tCallbac2 met(od to s&+nal
t(at &t (as completed. E/ents a%e use.ul .o% t(&s.
1.*.+ 5ow do I "re)ent concurrent access to my data?
Eac( ob;ect (as a concu%%ency loc2 @c%&t&cal sect&onA assoc&ated ,&t( &t. T(e 7ystem.T(%ead&n+.Mon&to%.Ente%CE9&t met(ods
a%e used to acMu&%e and %elease t(&s loc2. Fo% e9ample> &nstances o. t(e .ollo,&n+ class only allo, one t(%ead at a t&me to
ente% met(od .@A:
class C
{
public 6oi/ f()
{
try
{
&onitor.=nter(t'is);
...
}
finally
{
&onitor.=:it(t'is);
}
}
}
C# (as a 8loc28 2ey,o%d ,(&c( p%o/&des a con/en&ent s(o%t(and .o% t(e code abo/e:
class C
{
public 6oi/ f()
{
locB(t'is)
{
...
}
}
}
Note t(at call&n+ Mon&to%.Ente%@myOb;ectA does NOT mean t(at all access to myOb;ect &s se%&al&<ed. $t means t(at t(e
sync(%on&sat&on loc2 assoc&ated ,&t( myOb;ect (as been acMu&%ed> and no ot(e% t(%ead can acMu&%e t(at loc2 unt&l
Mon&to%.E9&t@oA &s called. $n ot(e% ,o%ds> t(&s class &s .unct&onally eMu&/alent to t(e classes abo/e:
class C
{
public 6oi/ f()
{
locB( mOob*ect )
{
...
}
}
pri6ate mOob*ect , ne2 ob*ect();
}
1.+ Tracin$
1.+.1 Is there #uilt%in su""ort (or tracin$Clo$$in$?
Qes> &n t(e 7ystem.*&a+nost&cs namespace. T(e%e a%e t,o ma&n classes t(at deal ,&t( t%ac&n+ *ebu+ and T%ace. T(ey
bot( ,o%2 &n a s&m&la% ,ay t(e d&..e%ence &s t(at t%ac&n+ .%om t(e *ebu+ class only ,o%2s &n bu&lds t(at (a/e t(e *E5J:
symbol de.&ned> ,(e%eas t%ac&n+ .%om t(e T%ace class only ,o%2s &n bu&lds t(at (a/e t(e T6ACE symbol de.&ned. Typ&cally
t(&s means t(at you s(ould use 7ystem.*&a+nost&cs.T%ace.'%&teL&ne .o% t%ac&n+ t(at you ,ant to ,o%2 &n debu+ and
%elease bu&lds> and 7ystem.*&a+nost&cs.*ebu+.'%&teL&ne .o% t%ac&n+ t(at you ,ant to ,o%2 only &n debu+ bu&lds.
1.+. Can I redirect tracin$ to a (ile?
Qes. T(e *ebu+ and T%ace classes bot( (a/e a L&stene%s p%ope%ty> ,(&c( &s a collect&on o. s&n2s t(at %ece&/e t(e t%ac&n+
t(at you send /&a *ebu+.'%&teL&ne and T%ace.'%&teL&ne %espect&/ely. 5y de.ault t(e L&stene%s collect&on conta&ns a s&n+le
s&n2> ,(&c( &s an &nstance o. t(e *e.aultT%aceL&stene% class. T(&s sends output to t(e '&n#! Output*ebu+7t%&n+@A .unct&on
and also t(e 7ystem.*&a+nost&cs.*ebu++e%.Lo+@A met(od. T(&s &s use.ul ,(en debu++&n+> but &. you8%e t%y&n+ to t%ace a
p%oblem at a custome% s&te> %ed&%ect&n+ t(e output to a .&le &s mo%e app%op%&ate. Fo%tunately> t(e Te9t'%&te%T%aceL&stene%
class &s p%o/&ded .o% t(&s pu%pose.
He%e8s (o, to use t(e Te9t'%&te%T%aceL&stene% class to %ed&%ect T%ace output to a .&le:
Trace.Listeners.Clear();
.ileStream fs , ne2 .ileStream( R"cSlo!.t:t"3 .ile&o/e.Create3 .ile#ccess.Write );
Trace.Listeners.#//( ne2 Te:tWriterTraceListener( fs ) );
Trace.WriteLine( R"T'is 2ill be 2riten to cSlo!.t:tN" );
Trace..lus'();
Note t(e use o. T%ace.L&stene%s.Clea%@A to %emo/e t(e de.ault l&stene%. $. you don8t do t(&s> t(e output ,&ll +o to t(e .&le and
Output*ebu+7t%&n+@A. Typ&cally t(&s &s not ,(at you ,ant> because Output*ebu+7t%&n+@A &mposes a b&+ pe%.o%mance (&t.
1.+.& Can I customise the trace out"ut?
Qes. Qou can ,%&te you% o,n T%aceL&stene%de%&/ed class> and d&%ect all output t(%ou+( &t. He%e8s a s&mple e9ample> ,(&c(
de%&/es .%om Te9t'%&te%T%aceL&stene% @and t(e%e.o%e (as &nbu&lt suppo%t .o% ,%&t&n+ to .&les> as s(o,n abo/eA and adds
t&m&n+ &n.o%mat&on and t(e t(%ead $* .o% eac( t%ace l&ne:
class &yListener Te:tWriterTraceListener
{
public &yListener( Stream s ) base(s)
{
}
public o6erri/e 6oi/ WriteLine( strin! s )
{
Writer.WriteLine( "{5$J} ;{?$G}< {E}"3
=n6ironment.TicBCount A mOstartTicBCount3
#pp$omain.+etCurrentT'rea/%/()3
s );
}
protecte/ int mOstartTicBCount , =n6ironment.TicBCount;
}
@Note t(at t(&s &mplementat&on &s not complete t(e T%aceL&stene%.'%&te met(od &s not o/e%%&dden .o% e9ample.A
T(e beauty o. t(&s app%oac( &s t(at ,(en an &nstance o. MyL&stene% &s added to t(e T%ace.L&stene%s collect&on> all calls to
T%ace.'%&teL&ne@A +o t(%ou+( MyL&stene%> &nclud&n+ calls made by %e.e%enced assembl&es t(at 2no, not(&n+ about t(e
MyL&stene% class.
1&. 0esources
1&.1 0ecommended #ooks
$ %ecommend t(e .ollo,&n+ boo2s> e&t(e% because $ pe%sonally l&2e t(em> o% because $ t(&n2 t(ey a%e ,ell %e+a%ded by ot(e%
.NET de/elope%s. @Note t(at $ +et a comm&ss&on .%om Ama<on &. you buy a boo2 a.te% .ollo,&n+ one o. t(ese l&n2s.A
Appl&ed M&c%oso.t .NET F%ame,o%2 E%o+%amm&n+ He..%ey 6&c(te%
Muc( ant&c&pated> ma&nly due to 6&c(te%8s supe%b '&n#! boo2s> and most people t(&n2 &t del&/e%s. T(e 8appl&ed8 &s a
l&ttle m&slead&n+ t(&s boo2 &s mostly about (o, t(e .NET F%ame,o%2 ,o%2s 8unde% t(e (ood8. E9amples a%e &n C#> but
t(e%e &s also a sepa%ate G5 ed&t&on o. t(e boo2.

Essent&al .NET Golume 1> T(e Common Lan+ua+e 6unt&me *on 5o9
*on8s boo2s don8t al,ays demonst%ate t(e same da<<l&n+ ab&l&ty to commun&cate t(at (e e9(&b&ts &n pe%son> but t(ey
a%e al,ays c(oc2 .ull o. tec(n&cal deta&l you ;ust don8t +et ot(e% places. Essent&al .NET &s l&2ely to become a must
%ead .o% all .NET de/elope%s.

E%o+%amm&n+ '&ndo,s ,&t( C# C(a%les Eet<old
Anot(e% sl&+(tly m&slead&n+ t&tle t(&s boo2 &s solely about :J$ p%o+%amm&n+ '&ndo,s Fo%ms and :*$D. 'ell
,%&tten> ,&t( comp%e(ens&/e co/e%a+e. My only @m&no%A c%&t&c&sm &s t(at t(e boo2 st&c2s closely to t(e .acts> ,&t(out
o..e%&n+ a +%eat deal &n t(e ,ay o. 8t&ps and t%&c2s8 .o% %eal,o%ld apps.

*e/elop&n+ Appl&cat&ons ,&t( G&sual 7tud&o.NET 6&c(a%d :%&mes
Co/e%s lots o. &nte%est&n+ top&cs t(at ot(e% boo2s don8t> &nclud&n+ ATL3> Mana+ed CDD> &nte%nat&onal&<at&on> %emot&n+>
as ,ell as t(e mo%e %uno.t(em&ll CL6 and C# stu... Also a lot o. &n.o on t(e G&sual 7tud&o $*E. T(&s boo2 &s most
su&table .o% %easonably e9pe%&enced CDD p%o+%amme%s.

C# and t(e .NET Elat.o%m And%e, T%oelsen
6e+a%ded by many as t(e best all %ound C#C.NET boo2. '&de co/e%a+e &nclud&n+ '&ndo,s Fo%ms> COM &nte%op>
A*O.NET> A7E.NET etc. T%oelsen also (as a %espected G5.NET boo2 called G&sual 5as&c .NET and t(e .NET
Elat.o%m: An Ad/anced :u&de.

E%o+%amm&n+ M&c%oso.t G&sual 5as&c .NET F%ancesco 5alena
5alena &s a %e2no,ned G5e%> and t(e %e/&e,s o. (&s G5.NET boo2 a%e +lo,&n+.

.NET and COM T(e Complete $nte%ope%ab&l&ty :u&de Adam Nat(an
'&dely %e+a%ded as t(e b&ble o. .NETCCOM &nte%op.

Ad/anced .NET 6emot&n+ $n+o 6amme%
'&dely %ecommended.

1&. Internet 0esources
T(e M&c%oso.t .NET (omepa+e &s at (ttp:CC,,,.m&c%oso.t.comCnetC. M&c%oso.t also (ost :OT*OTNET.
*e/= (ost t(e .NET Tone.
(ttp:CC,,,.cetusl&n2s.o%+CooPdotnet.(tml &s a supe%b set o. l&n2s to .NET %esou%ces.
C(%&s 7ells (as a +%eat set o. .NET l&n2s at (ttp:CC,,,.sellsb%ot(e%s.comCl&n2sC#manl&n2s.
C7(a%p.o%+
m&c%oso.t.publ&c.dotnet.U ne,s+%oups
My C# FAQ .o% CDD E%o+%amme%s.
1&.& We#lo$s
T(e .ollo,&n+ 'eblo+s (a/e %e+ula% .NET content:
T(e .NET :uy @5%ad '&lsonA
C(a%les Coo2 : *e/elope% o. =ML6EC.NET.
Ho(n Lam
Eete% *%ayton : Coaut(o% o. ?C# Essent&als? and ?C# &n a Nuts(ell?.
$n+o 6amme% : Aut(o% o. ?Ad/anced .NET %emot&n+?.
*%e, Ma%s(
Tomas 6est%epo
Hust&n 6udd
7&mon Fell : *e/elope% o. Eoc2et7OAE.
6&c(a%d Caetano
sellsb%ot(e%s.com: '&ndo,s *e/elope% Ne,s : Not %eally a blo+> but &ncludes %e+ula% .NET%elated ne,s.
1&.' 1am"le code F utilities
Lut< 6oede% (as some +%eat ut&l&t&es and l&b%a%&es at (ttp:CC,,,.a&sto.comC%oede%CdotnetC
Eete% *%ayton8s .NET :ood&es pa+e &s at (ttp:CC,,,.%a<o%so.t.netC
*on 5o9 F Hason '(&tt&n+ton8s dm.net COM mon&2e% at (ttp:CCsta...de/elop.comC;ason,Ccl%C%eadme.(tm
M&2e 'ood%&n+ (as some .NET samples at (ttp:CCsta...de/elop.comC,ood%&n+CdotnetC
C(a%les Coo28s =ML6EC.Net l&b%a%y &s a/a&lable at (ttp:CC,,,.coo2comput&n+.comC.
DotNetNut - Frequently Asked Questions
Q: What is the best way to set up an INI file for a VBNet
pro!ra"# $as this %han!ed in Net
A: If you want to provide your application with name-value pair type
information, you can use configuration files. A configuration file
must be located in the same directory as the executable and must
have the following naming convention: MyApplicat... more

Q: $ow %an i use late bindin! in VBnet I !et the na"e of the
%lass that needs to be instantiated at runti"e I know the
na"espa%e &whi%h is sa"e for all %lasses that are to be
dyna"i%ally instantiated'## In other words is thier any
equi(alent of %reateob)e%t&'
A: Loo into the Assembly.Load!" method and the
Activator.#reateInstance!" method. $here are several ways to
dynamically create ob%ects, you must decide which is best for you.

Q: Where %an I download (snet#
A: &ere's the lins: If your an M()* subscriber If you need to
purchase

Q: *an I ha(e V+N,- and the Visual +tudio ./ installed on the
sa"e "a%hine# If not how do I "aintain all "y le!a%y
appli%ations#
A: +es, $hey can be installed on the same machine without
interuption. -ile extentions are different also, so you don't have to
worry about you old .asp files opening in .(.*/$.

Q: Does N,- fully take pla%e of 0F*# Does it "ean that in the
%lose future 0F* would not be used e(en when writin! an
appli%ation not related to Internet#
A: $he intention of the .*/$ -ramewor was to provide developers
with a set of foundation classes to ease development. $o this end,
they have developed a framewor that encompasses the
functionallity of M-#, but contains so much more. $here is no
need ... more

Q: *an you e-"ail "e %ode to use with Visual +tudioN,-#
A: *o, but in late -ebuary we will be debuting the new )ot*et*ut site
implemented with the final version of the .*/$ -ramewor. 0ith the
new site you will be able to download source code as one of the
many new features.

Q: If 0-+ is the appli%ation ser(er for a distributed DNA
appli%ationwhat is the equi(alent in a N,- appli%ation#
A: M$( is not a re1uirement for the application server, or middle tier.
M$(, in it's latest form #2M3, provides services that can be
desirable such as distributed transactions, automatic transaction
enrollement, ob%ect pooling and such. If you desire t... more

S-ar putea să vă placă și