ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
¸»Ê¿¿µÐ¡ËµÍø ·µ»Ø±¾ÊéĿ¼ ¼ÓÈëÊéÇ© ÎÒµÄÊé¼Ü ÎÒµÄÊéÇ© TXTÈ«±¾ÏÂÔØ ¡ºÊղص½ÎÒµÄä¯ÀÀÆ÷¡»

VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ61²¿·Ö

¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡



section¡¡describes¡¡how¡¡to¡¡enable¡¡signing¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡CallRuntimeImplementation¡¡project¡¡is¡¡the¡¡user¡¡application¡¡and¡¡is¡¡responsible¡¡for¡¡calling¡¡¡¡

the¡¡functionality¡¡in¡¡Implementations1¡¡and¡¡Implementations2¡£¡¡What¡¡you¡¡need¡¡to¡¡consider¡¡very¡¡¡¡

carefully¡¡in¡¡the¡¡CallRuntimeImplementation¡¡project¡¡structure¡¡is¡¡that¡¡in¡¡the¡¡References¡¡node£»¡¡¡¡

only¡¡the¡¡Definitions¡¡project¡¡is¡¡present¡£¡¡It¡¡does¡¡not¡¡contain¡¡references¡¡to¡¡¡¡Implementations1¡¡or¡¡¡¡

Implementations2¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡It¡¡is¡¡important¡¡to¡¡understand¡¡that¡¡when¡¡a¡¡project¡¡does¡¡not¡¡have¡¡a¡¡reference¡¡to¡¡another¡¡¡¡

project£»¡¡it¡¡does¡¡not¡¡imply¡¡that¡¡the¡¡functionality¡¡cannot¡¡be¡¡used¡£¡¡To¡¡use¡¡the¡¡functionality¡¡of¡¡a¡¡¡¡

project¡¡that¡¡is¡¡not¡¡referenced£»¡¡you¡¡need¡¡to¡¡write¡¡code¡¡that¡¡will¡¡load¡¡the¡¡assembly¡¡dynamically¡£¡¡¡¡

With¡¡a¡¡dynamically¡¡loaded¡¡assembly£»¡¡you¡¡can¡¡do¡¡whatever¡¡is¡¡possible¡¡with¡¡a¡¡hard¡­linked¡¡reference¡£¡¡



¡öNote¡¡¡¡Remember¡¡that¡¡assemblies¡¡are¡¡the¡¡results¡¡of¡¡piling¡¡a¡¡project¡£¡¡A¡¡code¡¡library¡¡will¡¡produce¡¡a¡¡DLL¡¡¡¡

file£»¡¡while¡¡an¡¡executable¡¡project¡¡will¡¡produce¡¡an¡¡EXE¡¡file¡£¡¡¡¡



Signing¡¡an¡¡Assembly¡¡



Signing¡¡an¡¡assembly¡¡is¡¡giving¡¡the¡¡assembly¡¡a¡¡strong¡¡name£»¡¡which¡¡is¡¡a¡¡unique¡¡name¡£¡¡Think¡¡of¡¡it¡¡¡¡

as¡¡follows¡£¡¡My¡¡name¡¡is¡¡Christian¡¡Gross£»¡¡and¡¡on¡¡this¡¡planet¡¡we¡¡call¡¡Earth£»¡¡there¡¡are¡¡probably¡¡a¡¡¡¡

few¡¡dozen¡¡people¡¡with¡¡the¡¡name¡¡Christian¡¡Gross¡£¡¡Our¡¡governments¡¡distinguish¡¡between¡¡the¡¡¡¡

various¡¡people¡¡with¡¡my¡¡name¡¡by¡¡way¡¡of¡¡a¡¡passport¡£¡¡A¡¡passport¡¡is¡¡a¡¡unique¡¡identifier¡¡that¡¡converts¡¡¡¡

my¡¡mon¡¡name¡¡into¡¡a¡¡strong¡¡name¡£¡¡This¡¡is¡¡exactly¡¡what¡¡happens¡¡when¡¡you¡¡use¡¡strong¡¡names¡¡¡¡

with¡¡an¡¡assembly¡£¡¡A¡¡strong¡¡name¡¡is¡¡required¡¡when¡¡you¡¡want¡¡to¡¡add¡¡the¡¡assembly¡¡to¡¡the¡¡global¡¡¡¡

assembly¡¡cache¡¡£¨GAC£©¡£¡¡The¡¡GAC¡¡is¡¡where¡¡you¡¡can¡¡place¡¡an¡¡assembly¡¡to¡¡make¡¡it¡¡available¡¡for¡¡¡¡

global¡¡shared¡¡access¡£¡¡You¡¯ll¡¡learn¡¡more¡¡about¡¡the¡¡GAC¡¡in¡¡the¡¡¡°Loading¡¡a¡¡Strongly¡¡Named¡¡¡¡

Assembly¡±¡¡section¡¡later¡¡in¡¡this¡¡chapter¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡By¡¡default£»¡¡signing¡¡is¡¡not¡¡used¡£¡¡To¡¡enable¡¡signing£»¡¡you¡¡need¡¡to¡¡alter¡¡the¡¡properties¡¡of¡¡¡¡

the¡¡project¡£¡¡Follow¡¡these¡¡steps¡¡to¡¡enable¡¡signing¡¡for¡¡the¡¡Implementations2¡¡project¡¡and¡¡the¡¡¡¡

Definitions¡¡project£º¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡Right¡­click¡¡the¡¡project¡¡in¡¡the¡¡Solution¡¡Explorer¡¡and¡¡select¡¡Properties¡£¡¡



¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Click¡¡the¡¡Signing¡¡tab¡£¡¡



¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Check¡¡the¡¡Sign¡¡the¡¡Assembly¡¡check¡¡box¡£¡¡



¡¡¡¡¡¡¡¡¡¡4¡£¡¡¡¡Select¡¡¡¡from¡¡the¡¡bo¡¡box£»¡¡which¡¡pops¡¡up¡¡a¡¡dialog¡¡box¡¡allowing¡¡you¡¡to¡¡specify¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡file¡¡name¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡340¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

318¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡A¡¡PP¡¡L¡¡I¡¡CA¡¡TI¡¡O¡¡N¡¡¡¡¡¡CO¡¡N¡¡F¡¡I¡¡G¡¡U¡¡R¡¡AT¡¡IO¡¡N¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡D¡¡Y¡¡N¡¡A¡¡M¡¡I¡¡C¡¡¡¡L¡¡O¡¡AD¡¡I¡¡N¡¡G¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡5¡£¡¡¡¡Type¡¡in¡¡a¡¡key¡¡file¡¡name¡¡and¡¡a¡¡password£»¡¡and¡¡then¡¡click¡¡OK¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡6¡£¡¡¡¡Save¡¡your¡¡project¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Setting¡¡the¡¡Output¡¡Path¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡aim¡¡of¡¡the¡¡chapter¡¡is¡¡to¡¡demonstrate¡¡two¡¡things£º¡¡configuration¡¡files¡¡and¡¡the¡¡dynamic¡¡abil

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ities¡¡of¡¡¡£¡¡Explaining£»¡¡debugging£»¡¡and¡¡running¡¡the¡¡configuration¡¡source¡¡code¡¡is¡¡simple£»¡¡because¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡everything¡¡is¡¡laid¡¡out¡¡for¡¡you¡¡in¡¡the¡¡Visual¡¡Basic¡¡IDE¡£¡¡You¡¡will¡¡not¡¡run¡¡into¡¡any¡¡problems¡¡running¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡configuration¡¡code¡£¡¡However£»¡¡for¡¡the¡¡dynamic¡¡loading£»¡¡there¡¡is¡¡a¡¡plication¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡As¡¡you¡¯ve¡¡seen£»¡¡the¡¡¡¡CallRuntimeImplementation¡¡project¡¡does¡¡not¡¡have¡¡explicit¡¡references¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡Implementations1¡¡and¡¡Implementations2¡£¡¡This¡¡means¡¡that¡¡if¡¡the¡¡code¡¡from¡¡Implementations1¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡Implementations2¡¡is¡¡referenced¡¡from¡¡CallRuntimeImplementations¡¡using¡¡dynamic¡¡techniques£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Visual¡¡Basic¡¡Express¡¡will¡¡have¡¡no¡¡clue¡¡as¡¡to¡¡what¡¡you¡¡are¡¡doing¡£¡¡You¡¡might¡¡argue¡¡that¡¡there¡¡is¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡one¡¡Visual¡¡Basic¡¡Express¡¡solution£»¡¡and¡¡Implementations1¡¡and¡¡¡¡Implementations2¡¡are¡¡part¡¡of¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡overall¡¡project£»¡¡but¡¡unless¡¡an¡¡explicit¡¡reference¡¡is¡¡made£»¡¡Visual¡¡Basic¡¡Express¡¡does¡¡not¡¡know¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡about¡¡this¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡However£»¡¡you¡¡can¡¡overe¡¡this¡¡problem¡¡fairly¡¡easily¡¡by¡¡changing¡¡where¡¡the¡¡projects¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡place¡¡their¡¡piled¡¡output¡£¡¡As¡¡a¡¡habit£»¡¡I¡¡often¡¡make¡¡all¡¡projects¡¡build¡¡to¡¡a¡¡central¡¡directory¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡can¡¡set¡¡the¡¡output¡¡directory¡¡in¡¡the¡¡pile¡¡tab¡¡of¡¡a¡¡project¡¯s¡¡Properties¡¡window£»¡¡as¡¡shown¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡Figure¡¡12¡­2¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Figure¡¡12¡­2¡£¡¡Setting¡¡the¡¡build¡¡output¡¡path¡¡to¡¡a¡¡mon¡¡path¡¡£¨¡£¡£bin£©¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡You¡¡need¡¡to¡¡set¡¡the¡¡Build¡¡Output¡¡Path¡¡field¡¡to¡¡a¡¡mon¡¡location¡¡for¡¡all¡¡projects¡£¡¡When¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡do¡¡that£»¡¡a¡¡build¡¡will¡¡result¡¡in¡¡a¡¡directory¡¡structure¡¡like¡¡that¡¡shown¡¡in¡¡Figure¡¡12¡­3¡¡£¨in¡¡the¡¡next¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡section£©¡£¡¡With¡¡all¡¡of¡¡the¡¡files¡¡in¡¡a¡¡mon¡¡directory£»¡¡running¡¡the¡¡dynamic¡­loading¡¡routines¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bees¡¡trivial¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡341¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡AP¡¡P¡¡L¡¡I¡¡CAT¡¡I¡¡ON¡¡¡¡¡¡CO¡¡N¡¡F¡¡IG¡¡U¡¡R¡¡AT¡¡IO¡¡N¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡D¡¡Y¡¡N¡¡A¡¡M¡¡IC¡¡¡¡¡¡L¡¡O¡¡AD¡¡IN¡¡G¡¡319¡¡



Defining¡¡and¡¡Processing¡¡a¡¡Configuration¡¡File¡¡



A¡¡configuration¡¡file¡¡is¡¡a¡¡file¡¡that¡¡contains¡¡information¡¡about¡¡how¡¡a¡¡program¡¡should¡¡behave¡£¡¡¡¡

Configuration¡¡files¡¡by¡¡themselves¡¡do¡¡not¡¡control¡¡how¡¡a¡¡program¡¡behaves¡£¡¡For¡¡a¡¡configuration¡¡¡¡

file¡¡to¡¡influence¡¡the¡¡behavior¡¡of¡¡a¡¡program£»¡¡the¡¡program¡¡needs¡¡to¡¡read¡¡and¡¡act¡¡on¡¡the¡¡informa

tion¡¡in¡¡the¡¡configuration¡¡file¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Using¡¡a¡¡configuration¡¡file¡¡is¡¡not¡¡as¡¡easy¡¡as¡¡you¡¡may¡¡think¡¡it¡¡is¡£¡¡Yes£»¡¡it¡¡is¡¡easy¡¡to¡¡define¡¡a¡¡¡¡

configuration¡¡file¡¡and¡¡read¡¡the¡¡configuration¡¡file¡£¡¡What¡¡is¡¡difficult¡¡is¡¡how¡¡to¡¡define¡¡where¡¡¡¡

the¡¡configuration¡¡file¡¡is¡¡located¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Suppose¡¡you¡¡have¡¡an¡¡application¡¡that¡¡is¡¡installed¡¡on¡¡the¡¡hard¡¡disk£»¡¡and¡¡the¡¡application¡¡¡¡

must¡¡make¡¡an¡¡assumption¡¡about¡¡where¡¡the¡¡configuration¡¡file¡¡is¡¡stored¡£¡¡One¡¡assumption¡¡could¡¡¡¡

be¡¡the¡¡root¡¡directory¡¡of¡¡the¡¡C£º¡¡drive¡£¡¡Yet¡¡that¡¡could¡¡be¡¡incorrect£»¡¡because¡¡some¡¡puters¡¡don¡¯t¡¡¡¡

have¡¡C£º¡¡as¡¡the¡¡root¡¡drive¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡solves¡¡this¡¡problem¡¡in¡¡an¡¡interesting¡¡way£º¡¡whatever¡¡the¡¡executing¡¡application¡¡is¡¡called£»¡¡¡¡

the¡¡configuration¡¡file¡¡has¡¡the¡¡same¡¡name¡¡with¡¡a¡¡¡¡¡£config¡¡extension¡£¡¡Figure¡¡12¡­3¡¡shows¡¡an¡¡example£»¡¡¡¡

in¡¡which¡¡the¡¡console¡¡application¡¡is¡¡CallRuntimeImplementation¡£exe¡£¡¡The¡¡configuration¡¡file¡¡is¡¡¡¡

CallRuntimeImplementation¡£exe¡£config¡£¡¡A¡¡configuration¡¡file¡¡and¡¡the¡¡assembly¡¡that¡¡it¡¡relates¡¡to¡¡¡¡

are¡¡located¡¡in¡¡the¡¡same¡¡directory¡£¡¡



Figure¡¡12¡­3¡£¡¡A¡¡console¡¡application¡¡and¡¡its¡¡¡¡associated¡¡configuration¡¡file¡¡



¡öNote¡¡¡¡Naming¡¡the¡¡configuration¡¡file¡¡using¡¡a¡¡bination¡¡of¡¡the¡¡assembly¡¯s¡¡application¡¡name¡¡with¡¡the¡¡¡¡

¡¡¡£config¡¡extension¡¡is¡¡an¡¡example¡¡of¡¡a¡¡convention¡¡architecture¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡section£»¡¡you¡¯ll¡¡see¡¡how¡¡to¡¡write¡¡a¡¡configuration¡¡file¡¡and¡¡then¡¡reference¡¡it¡¡within¡¡an¡¡¡¡

application¡£¡¡This¡¡setup¡¡will¡¡use¡¡the¡¡configuration¡¡file¡¡whenever¡¡a¡¡request¡¡for¡¡a¡¡certain¡¡piece¡¡of¡¡¡¡

functionality¡¡is¡¡made¡¡and¡¡supply¡¡the¡¡exact¡¡location¡¡of¡¡the¡¡assembly¡£¡¡



Creating¡¡an¡¡XML¡­Based¡¡Configuration¡¡File¡¡



The¡¡configuration¡¡file¡¡contains¡¡Extensible¡¡Markup¡¡Language¡¡£¨XML£©¡£¡¡XML¡¡is¡¡a¡¡way¡¡of¡¡structuring¡¡¡¡

a¡¡text¡¡file¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡Consider¡¡the¡¡following¡¡text¡¡structure¡¡using¡¡spaces¡£¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡342¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

320¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡A¡¡PP¡¡L¡¡I¡¡CA¡¡TI¡¡O¡¡N¡¡¡¡¡¡CO¡¡N¡¡F¡¡I¡¡G¡¡U¡¡R¡¡AT¡¡IO¡¡N¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡D¡¡Y¡¡N¡¡A¡¡M¡¡I¡¡C¡¡¡¡L¡¡O¡¡AD¡¡I¡¡N¡¡G¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡First¡¡Element¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Second¡¡Element¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Third¡¡Element¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡First¡¡Element¡¡is¡¡a¡¡parent£»¡¡and¡¡¡¡Second¡¡Element¡¡and¡¡Third¡¡Element¡¡are¡¡subelements¡¡of¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡parent¡£¡¡The¡¡structuring¡¡of¡¡the¡¡parent¡¡and¡¡child¡¡elements¡¡is¡¡fragile£»¡¡to¡¡say¡¡the¡¡least¡£¡¡We¡¡need¡¡a¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡more¡¡robust¡¡way¡¡of¡¡structuring¡¡the¡¡data£»¡¡which¡¡is¡¡where¡¡XML¡¡es¡¡in¡£¡¡Think¡¡of¡¡XML¡¡as¡¡having¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡ability¡¡to¡¡define¡¡folders¡¡and¡¡folders¡¡within¡¡folders¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Let¡¯s¡¡define¡¡the¡¡XML¡¡that¡¡makes¡¡up¡¡a¡¡¡¡application¡¡configuration¡¡file¡£¡¡All¡¡¡¡applica

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡tion¡¡configuration¡¡files¡¡can¡¡be¡¡created¡¡using¡¡Visual¡¡Basic¡¡Express£»¡¡as¡¡follows£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡1¡£¡¡¡¡Right¡­click¡¡the¡¡CallRuntimeImplementation¡¡project¡¡and¡¡select¡¡Add¡¡¡¡New¡¡Item¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡2¡£¡¡¡¡Select¡¡Text¡¡File¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡3¡£¡¡¡¡Name¡¡the¡¡file¡¡App¡£config¡¡and¡¡click¡¡Add¡£¡¡The¡¡App¡£config¡¡file¡¡is¡¡created¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡It¡¡is¡¡fine¡¡to¡¡leave¡¡the¡¡name¡¡as¡¡is£»¡¡because¡¡when¡¡your¡¡¡¡application¡¡is¡¡piled£»¡¡the¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡application¡¡configuration¡¡file¡¡will¡¡be¡¡renamed¡¡and¡¡placed¡¡beside¡¡the¡¡¡¡application¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡¡¡application¡¡configuration¡¡files¡¡apply¡¡to¡¡assemblies¡£¡¡Thus£»¡¡if¡¡an¡¡assembly¡¡uses¡¡the¡¡config

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡uration¡¡API£»¡¡the¡¡configuration¡¡file¡¡applied¡¡will¡¡be¡¡the¡¡one¡¡associated¡¡with¡¡the¡¡calling¡¡application¡£¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Open¡¡the¡¡new¡¡App¡£config¡¡file¡¡and¡¡add¡¡the¡¡following¡¡XML£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡line¡¡is¡¡called¡¡an¡¡XML¡¡declaration£»¡¡and¡¡it¡¡can¡¡specify¡¡the¡¡encoding¡¡that¡¡will¡¡be¡¡used¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨plus¡¡other¡¡information£»¡¡which¡¡we¡¡won¡¯t¡¡need¡¡to¡¡concern¡¡ourselves¡¡with¡¡here£©¡£¡¡The¡¡second¡¡and¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡third¡¡lines¡¡define¡¡a¡¡root¡¡XML¡¡node¡¡£¨also¡¡called¡¡an¡¡XML¡¡element£©¡£¡¡Think¡¡of¡¡a¡¡root¡¡XML¡¡node¡¡as¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡analogous¡¡to¡¡a¡¡root¡¡folder¡¡where¡¡all¡¡of¡¡the¡¡other¡¡folders¡¡will¡¡be¡¡stored¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡root¡¡node¡¡is¡¡started¡¡with¡¡the¡¡identifier¡¡configuration¡¡surrounded¡¡by¡¡angle¡¡brackets¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡node¡¡is¡¡terminated¡¡by¡¡the¡¡same¡¡identifier¡¡and¡¡angle¡¡brackets£»¡¡with¡¡the¡¡addition¡¡of¡¡a¡¡slash¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡prefix¡¡to¡¡the¡¡identifier¡£¡¡The¡¡only¡¡place¡¡where¡¡another¡¡XML¡¡node¡¡can¡¡be¡¡placed¡¡is¡¡between¡¡the¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡beginning¡¡and¡¡ending¡¡XML¡¡nodes¡£¡¡For¡¡example£»¡¡this¡¡XML¡¡is¡¡invalid£º¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡the¡¡example£»¡¡¡¡is¡¡started¡¡inside¡¡the¡¡declaration¡¡of¡¡£»¡¡but¡¡its¡¡termi

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡nation£»¡¡£»¡¡is¡¡outside¡¡the¡¡declaration¡¡of¡¡¡£¡¡¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡343¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡AR¡¡N¡¡IN¡¡G¡¡¡¡¡¡AB¡¡O¡¡U¡¡T¡¡¡¡¡¡AP¡¡P¡¡L¡¡I¡¡CAT¡¡I¡¡ON¡¡¡¡¡¡CO¡¡N¡¡F¡¡IG¡¡U¡¡R¡¡AT¡¡IO¡¡N¡¡¡¡¡¡A¡¡N¡¡D¡¡¡¡¡¡D¡¡Y¡¡N¡¡A¡¡M¡¡IC¡¡¡¡¡¡L¡¡O¡¡AD¡¡IN¡¡G¡¡321¡¡



Adding¡¡the¡¡Dynamic¡¡Loading¡¡Configuration¡¡Items¡¡



After¡¡you¡¯ve¡¡added¡¡a¡¡configuration¡¡file£»¡¡the¡¡next¡¡step¡¡is¡¡to¡¡add¡¡the¡¡elements¡¡that¡¡will¡¡be¡¡used¡¡by¡¡¡¡

the¡¡dynamic¡¡loader¡£¡¡The¡¡idea¡¡will¡¡be¡¡to¡¡define¡¡some¡¡abstract¡¡identifier¡¡that¡¡will¡¡be¡¡cross¡­referenced¡¡¡¡

to¡¡a¡¡type¡¡and¡¡assembly¡£¡¡Thus£»¡¡to¡¡load¡¡Implementations1¡£Implementation£»¡¡we¡¯ll¡¡use¡¡the¡¡identifier¡¡¡¡

Impl1£»¡¡type¡¡Implementations1¡£Implementation£»¡¡and¡¡assembly¡¡Implementations1¡£dll¡£¡¡Similarly£»¡¡¡¡

Impl2¡¡will¡¡cross¡­reference¡¡to¡¡the¡¡type¡¡¡¡Implementations2¡£Implementation¡¡and¡¡the¡¡assembly¡¡¡¡

Implementations2¡£dll¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Modify¡¡the¡¡configuration¡¡file¡¡as¡¡shown¡¡here£»¡¡adding¡¡the¡¡bolded¡¡code¡£¡¡



¡¡

¡¡

¡¡¡¡¡¡

¡¡¡¡¡¡¡¡¡¡

¡¡¡¡¡¡



¡¡



¡¡¡¡¡¡¡¡¡¡The¡¡XML¡¡node¡¡£»¡¡which¡¡you¡¡could¡¡think¡¡of¡¡as¡¡adding¡¡the¡¡folder¡¡appSettings£»¡¡¡¡

contains¡¡XML¡¡nodes¡¡with¡¡the¡¡identifier¡¡¡£¡¡This¡¡node¡¡defines¡¡a¡¡section¡¡in¡¡the¡¡configuration¡¡¡¡

file¡¡that¡¡contains¡¡application¡¡settings¡¡expressed¡¡as¡¡key/value¡¡pairs¡£¡¡Each¡¡key/value¡¡pair¡¡is¡¡defined¡¡¡¡

in¡¡the¡¡XML¡¡node¡¡using¡¡the¡¡XML¡¡attributes¡¡key¡¡and¡¡value¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡Notice¡¡that¡¡the¡¡XML¡¡node¡¡¡¡has¡¡a¡¡starting¡¡element£»¡¡but¡¡not¡¡an¡¡ending¡¡element¡£¡¡This¡¡is¡¡¡¡

because¡¡the¡¡¡¡node¡¡is¡¡terminated¡¡with¡¡an¡¡angle¡¡bracket¡¡prefixed¡¡with¡¡a¡¡slash¡£¡¡This¡¡means¡¡¡¡

that¡¡the¡¡ending¡¡element¡¡is¡¡not¡¡necessary¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡configuration¡¡file¡¡defines¡¡a¡¡key¡¡Assemblies¡¡and¡¡a¡¡value¡¡that¡¡contains¡¡a¡¡buffer¡¡of¡¡ma

separated¡¡identifiers¡£¡¡The¡¡identifiers¡¡represent¡¡the¡¡identifier£»¡¡type£»¡¡and¡¡assembly¡¡of¡¡supported¡¡¡¡

dynamically¡¡loaded¡¡assemblies¡£¡¡

¡¡¡¡¡¡¡¡¡¡The¡¡configuration¡¡file¡¡is¡¡plete£»¡¡but¡¡will¡¡not¡¡influence¡¡the¡¡behavior¡¡of¡¡the¡¡application£»¡¡¡¡

because¡¡source¡¡code¡¡must¡¡be¡¡added¡¡to¡¡the¡¡application¡¡that¡¡reads¡¡the¡¡configuration¡¡file¡¡settings£»¡¡¡¡

as¡¡described¡¡in¡¡the¡¡next¡¡section¡£¡¡



¡öNote¡¡¡¡For¡¡the¡¡examples¡¡in¡¡this¡¡chapter£»¡¡use¡¡the¡¡XML¡¡shown¡¡in¡¡the¡¡listings¡£¡¡XML¡¡is¡¡not¡¡difficult¡¡to¡¡learn£»¡¡but¡¡¡¡

for¡¡now£»¡¡you¡¯ll¡¡get¡¡by¡¡with¡¡what¡¡I¡¡show¡¡you¡¡here¡£¡¡For¡¡more¡¡information¡¡about¡¡XML£»¡¡visit¡¡the¡¡MSDN¡¡XML¡¡Developer¡¡¡¡

Center¡¡at¡¡http£º//msdn2¡£microsoft¡£/en¡­us/xml/default¡£aspx¡£¡¡



Reading¡¡a¡¡Configuration¡¡File¡¡



Reading¡¡a¡¡configuration¡¡file¡¡is¡¡very¡¡simple¡¡because¡¡the¡¡¡¡Framework¡¡es¡¡with¡¡an¡¡easy

to¡­use¡¡configuration¡¡API¡£¡¡For¡¡example£»¡¡to¡¡read¡¡the¡¡value¡¡for¡¡the¡¡key¡¡Assemblies£»¡¡use¡¡the¡¡following¡¡¡¡

code£»¡¡which¡¡would¡¡be¡¡added¡¡to¡¡your¡¡application¡¡when¡¡the¡¡application¡¡first¡¡starts£»¡¡such¡¡as¡¡in¡¡the¡¡¡¡

Main£¨£©¡¡method¡¡of¡¡a¡¡console¡¡program¡£¡¡



Dim¡¡value¡¡As¡¡String¡¡=¡¡_¡¡

¡¡¡¡System¡£Configuration¡£ConfigurationManager¡£AppSettings£¨¡¨Assemblies¡¨£©¡¡


¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­Page¡¡344¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­¡­

322¡¡¡¡¡¡¡¡¡¡¡¡¡¡CH¡¡AP¡¡T¡¡E¡¡R¡¡¡¡¡¡1¡¡2¡¡¡¡¡¡¡ö¡¡¡¡¡¡¡¡L¡¡E¡¡A¡¡R¡¡N¡¡I¡¡N¡¡G¡¡¡¡¡¡A¡¡B¡¡OU¡¡T¡¡¡¡¡¡A¡¡PP¡¡L¡¡I¡¡CA¡¡TI¡¡O¡¡N¡¡¡¡¡¡CO¡¡N¡¡F¡¡I¡¡G¡¡U¡¡R¡¡AT¡¡IO¡¡N¡¡¡¡¡¡AN¡¡D¡¡¡¡¡¡D¡¡Y¡¡N¡¡A¡¡M¡¡I¡¡C¡¡¡¡L¡¡O¡¡AD¡¡I¡¡N¡¡G¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ConfigurationManager¡¡is¡¡a¡¡shared¡¡class¡¡that¡¡provides¡¡the¡¡entry¡¡point¡¡to¡¡reading¡¡items¡¡from¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡a¡¡configuration¡¡file¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡this¡¡example£»¡¡a¡¡couple¡¡assumptions¡¡have¡¡been¡¡made¡£¡¡The¡¡first¡¡assumption¡¡is¡¡that¡¡you¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡want¡¡to¡¡read¡¡the¡¡configuration¡¡settings¡¡from¡¡the¡¡application¡¡that¡¡is¡¡currently¡¡executing¡£¡¡The¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡second¡¡assumption¡¡is¡¡that¡¡you¡¡want¡¡to¡¡read¡¡the¡¡configuration¡¡items¡¡stored¡¡within¡¡the¡¡XML¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡node¡¡¡£¡¡Based¡¡on¡¡those¡¡two¡¡assumptions£»¡¡the¡¡settings¡¡are¡¡stored¡¡in¡¡a¡¡shared¡¡prop

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡erty¡¡named¡¡AppSettings£»¡¡which¡¡returns¡¡an¡¡instance¡¡of¡¡type¡¡NameValueCollection¡£¡¡£¨The¡¡way¡¡that¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡AppSettings¡¡is¡¡referenced¡¡makes¡¡it¡¡appear¡¡as¡¡if¡¡AppSettings¡¡were¡¡an¡¡indexer£»¡¡which¡¡it¡¡is¡¡not¡££©¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡When¡¡retrieving¡¡the¡¡buffer¡¡of¡¡ma¡­separated¡¡identifiers¡¡using¡¡¡¡ConfigurationManager£»¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡need¡¡to¡¡parse¡¡the¡¡buffer¡¡and¡¡then¡¡make¡¡sense¡¡of¡¡the¡¡information£»¡¡as¡¡described¡¡in¡¡the¡¡next¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡section¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dynamically¡¡Loading¡¡an¡¡Assembly¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡In¡¡¡¡terms£»¡¡to¡¡use¡¡the¡¡configuration¡¡file¡¡with¡¡a¡¡dynamically¡¡loaded¡¡application£»¡¡you¡¡need¡¡to¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡load¡¡the¡¡assembly£»¡¡and¡¡from¡¡the¡¡assembly£»¡¡instantiate¡¡the¡¡type¡£¡¡You¡¡can¡¡reference¡¡assemblies¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡locally¡¡and¡¡also¡¡reference¡¡them¡¡from¡¡the¡¡GAC¡£¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dynamically¡¡Instantiating¡¡a¡¡Type¡¡



¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡code¡¡to¡¡dynamically¡¡instantiate¡¡a¡¡type¡¡requires¡¡parsing¡¡the¡¡ma¡­separated¡¡buffer¡¡into¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡its¡¡respective¡¡identifiers¡£¡¡To¡¡keep¡¡things¡¡organized£»¡¡the¡¡three¡¡pieces¡¡of¡¡information¡¡are¡¡stored¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡in¡¡a¡¡class¡£¡¡This¡¡can¡¡be¡¡called¡¡a¡¡data¡¡class¡¡because¡¡it¡¡has¡¡only¡¡data¡¡members¡£¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡data¡¡class¡¡is¡¡a¡¡placeholder£»¡¡needed¡¡only¡¡by¡¡the¡¡code¡¡used¡¡to¡¡dynamically¡¡load¡¡the¡¡type¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡makes¡¡it¡¡possible¡¡to¡¡define¡¡the¡¡data¡¡class¡¡as¡¡a¡¡private¡¡class£»¡¡because¡¡a¡¡private¡¡class¡¡implies¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡only¡¡the¡¡parent¡¡class¡¡can¡¡instantiate¡¡it¡£¡¡¡¡

¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡With¡¡the¡¡ConfigurationLoader¡¡class¡¡defined¡¡as¡¡the¡¡class¡¡used¡¡to¡¡dynamically¡¡instantiate¡¡
·µ»ØĿ¼ ÉÏÒ»Ò³ ÏÂÒ»Ò³ »Øµ½¶¥²¿ ÔÞ£¨11£© ²È£¨11£©
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·­Ò³ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾­Ñé½±Àø£¬ÈÏÕæдԭ´´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾­Ñé½±ÀøŶ£¡