Tải bản đầy đủ - 0 (trang)
Chapter 17.  Assemblies and Versioning

Chapter 17.  Assemblies and Versioning

Tải bản đầy đủ - 0trang

17.1.PEFiles

Ondisk,assembliesarePortableExecutable(PE)files.PEfiles

aren'tnew.Theformatofa.NETPEfileisexactlythesameasa

normalWindowsPEfile.PEfilesareimplementedasDLLsor

EXEs.

Physically,assembliesconsistofoneormoremodules.Modules

aretheconstituentpiecesofassemblies.Standingalone,

modulescan'tbeexecuted;theymustbecombinedinto

assembliestobeuseful.

Youwilldeployandreusetheentirecontentsofanassemblyas

aunit.Assembliesareloadedondemand,andwillnotbe

loadedifnotneeded.



17.2.Metadata

Metadataisinformationstoredintheassemblythatdescribes

thetypesandmethodsoftheassemblyandprovidesother

usefulinformationabouttheassembly.Assembliesaresaidto

beself-describingbecausethemetadatafullydescribesthe

contentsofeachmodule.Metadataisdiscussedindetailin

Chapter18.



17.3.SecurityBoundary

Assembliesformsecurityboundariesaswellastype

boundaries.Thatis,anassemblyisthescopeboundaryforthe

typesitcontains,andtypedefinitionscan'tcrossassemblies.

Youcan,ofcourse,refertotypesacrossassemblyboundaries

byaddingareferencetotherequiredassembly,eitherinthe

IDEoronthecommandline,atcompiletime.Whatyoucan't

doishavethedefinitionofatypespantwoassemblies.

Theinternalaccessmodifierlimitsaccess(foramethod,for

example)tothecurrentassembly.



17.4.Manifests

Aspartofitsmetadata,everyassemblyhasamanifest.This

describeswhatisintheassembly:identificationinformation

(name,version,etc.),alistofthetypesandresourcesinthe

assembly,alistofmodules,amaptoconnectpublictypeswith

theimplementingcode,andalistofassembliesreferencedby

thisassembly.

Eventhesimplestprogramhasamanifest.Youcanexamine

thatmanifestusingILDasm,whichisprovidedaspartofyour

developmentenvironment.Whenyouopenthemanifestin

ILDasm,theEXEprogramcreatedbyExample12-3lookslike

Figure17-1.



Figure17-1.ILDasmofExample12-3



Noticethemanifest(secondlinefromthetop).Double-clicking

themanifestopensaManifestwindow,asshowninFigure17-2.



Figure17-2.TheManifestwindow



Thisfileservesasamapofthecontentsoftheassembly.You

canseeinthefirstlinethereferencetothemscorlibassembly,

whichisreferencedbythisandevery.NETapplication.The

mscorlibassemblyisthecorelibraryassemblyfor.NETandis

availableonevery.NETplatform.

Thenextassemblylineisareferencetotheassemblyfrom

Example12-3.Youcanalsoseethatthisassemblyconsistsofa

singlemodule.Youcanignoretherestofthemetadatafornow.



17.5.MultimoduleAssemblies

Assembliescanconsistofmorethanonemodule,thoughthis

isn'tsupportedbyVisualStudio2005.

Asingle-moduleassemblyhasasinglefilethatcanbeanEXE

orDLLfile.Thissinglemodulecontainsallthetypesand

implementationsfortheapplication.Theassemblymanifestis

embeddedwithinthismodule.

Eachmodulehasamanifestofitsownthatisseparatefromthe

assemblymanifest.Themodulemanifestliststheassemblies

referencedbythatparticularmodule.Inaddition,ifthemodule

declaresanytypes,thesearelistedinthemanifestalongwith

thecodetoimplementthemodule.Amodulecanalsocontain

resources,suchastheimagesneededbythatmodule.

Amultimoduleassemblyconsistsofmultiplefiles(zeroorone

EXEandzeroormoreDLLfiles,thoughyoumusthaveatleast

oneEXEorDLL).Theassemblymanifestinthiscasecanreside

inastandalonefile,oritcanbeembeddedinoneofthe

modules.Whentheassemblyisreferenced,theruntimeloads

thefilecontainingthemanifestandthenloadstherequired

modulesasneeded.



17.5.1.BuildingaMultimoduleAssembly

Todemonstratetheuseofmultimoduleassemblies,the

followingexamplecreatesacoupleofverysimplemodulesthat

youcanthencombineintoasingleassembly.Thefirstmodule

isaFractionclass.Thissimpleclasswillallowyoutocreate

andmanipulatecommonfractions.Example17-1illustrates.



Example17-1.TheFractionclass



#regionUsingdirectives

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

#endregion

namespaceProgCS

{

publicclassFraction

{

privateintnumerator;

privateintdenominator;

publicFraction(intnumerator,intdenominator)

{

this.numerator=numerator;

this.denominator=denominator;

}

publicFractionAdd(Fractionrhs)

{

if(rhs.denominator!=this.denominator)

{

returnnewFraction(

rhs.denominator*numerator+

rhs.numerator*denominator,

denominator*rhs.denominator);

}

returnnewFraction(

this.numerator+rhs.numerator,

this.denominator);

}

publicoverridestringToString()



{

returnnumerator+"/"+denominator;

}

}

}



NoticethattheFractionclassisintheProgCSnamespace.The

fullnamefortheclassisProgCS.Fraction.

TheFractionclasstakestwovaluesinitsconstructor:a

numeratorandadenominator.ThereisalsoanAdd()

method,whichtakesasecondFractionandreturnsthesum,

assumingthetwoshareacommondenominator.Thisclassis

simplistic,butitwilldemonstratethefunctionalitynecessaryfor

thisexample.

ThesecondclassistheMyCalcclass,whichstandsinfora

robustcalculator.Example17-2illustrates.



Example17-2.Thecalculator

#regionUsingdirectives

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

#endregion

namespaceProgCS

{

publicclassMyCalc

{

publicintAdd(intval1,intval2)



{

returnval1+val2;

}

publicintMult(intval1,intval2)

{

returnval1*val2;

}

}

}



Onceagain,MyCalcisaverystripped-downclasstokeepthings

simple.NoticethatMyCalcisalsointheProgCSnamespace.

Thisissufficienttocreateanassembly.UseanAssemblyInfo.cs

filetoaddsomemetadatatotheassembly.Theuseof

metadataiscoveredinChapter18.



YoucanwriteyourownAssemblyInfo.csfile,butthesimplestapproach

istoletVisualStudiogenerateoneforyouautomatically.



VisualStudiocreatesonlysingle-moduleassemblies.

Youcancreateamultimoduleresourcewiththe/addModules

command-lineoption.Theeasiestwaytocompileandbuilda

multimoduleassemblyiswithamakefile,whichyoucancreate

withNotepadoranytexteditor.



Ifyouareunfamiliarwithmakefiles,don'tworry;thisistheonly

examplethatneedsamakefile,andthatisjusttogetaroundthe

currentlimitationofVisualStudiocreatingonlysingle-module

assemblies.Ifnecessary,youcanjustusethemakefileasoffered

withoutfullyunderstandingeveryline.Formoreinformation,see



ManagingProjectswithmake(O'Reilly).



Example17-3showsthecompletemakefile(whichisexplained

indetailimmediatelyafterward).Torunthisexample,putthe

makefile(withthenamemakefile)inadirectorytogetherwitha

copyofCalc.cs,Fraction.cs,andAssemblyInfo.cs.Startupa

.NETcommandwindowandcdtothatdirectory.Invokenmake

withoutanycommandswitch.Youwillfindthe

SharedAssembly.dllinthe\binsubdirectory.



Example17-3.Thecompletemakefilefora

multimoduleassembly

ASSEMBLY=MySharedAssembly.dll

BIN=.\bin

SRC=.

DEST=.\bin

CSC=csc/nologo/debug+/d:DEBUG/d:TRACE

MODULETARGET=/t:module

LIBTARGET=/t:library

EXETARGET=/t:exe

REFERENCES=System.dll

MODULES=$(DEST)\Fraction.dll$(DEST)\Calc.dll

METADATA=$(SRC)\AssemblyInfo.cs

all:$(DEST)\MySharedAssembly.dll

#Assemblymetadataplacedinsamemoduleasmanifest



$(DEST)\$(ASSEMBLY):$(METADATA)$(MODULES)$(DEST)

$(CSC)$(LIBTARGET)/addmodule:$(MODULES:=;)/out:$@%s

#AddCalc.dllmoduletothisdependencylist

$(DEST)\Calc.dll:Calc.cs$(DEST)

$(CSC)$(MODULETARGET)/r:$(REFERENCES:=;)/out:$@%s

#AddFraction

$(DEST)\Fraction.dll:Fraction.cs$(DEST)

$(CSC)$(MODULETARGET)/r:$(REFERENCES:=;)/out:$@%s

$(DEST)::

!if!EXISTS($(DEST))

mkdir$(DEST)

!endif



Themakefilebeginsbydefiningtheassemblyyouwantto

build:

ASSEMBLY=MySharedAssembly.dll



Itthendefinesthedirectoriesyou'lluse,puttingtheoutputina

bindirectorybeneaththecurrentdirectoryandretrievingthe

sourcecodefromthecurrentdirectory:

SRC=.

DEST=.\bin



Buildtheassemblyasfollows:

$(DEST)\$(ASSEMBLY):$(METADATA)$(MODULES)$(DEST)



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 17.  Assemblies and Versioning

Tải bản đầy đủ ngay(0 tr)

×