ÓÑÇéÌáʾ£ºÈç¹û±¾ÍøÒ³´ò¿ªÌ«Âý»òÏÔʾ²»ÍêÕû£¬Çë³¢ÊÔÊó±êÓÒ¼ü¡°Ë¢Ð¡±±¾ÍøÒ³£¡
VB2008´ÓÈëÃŵ½¾«Í¨(PDF¸ñʽӢÎÄ°æ)-µÚ85²¿·Ö
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿! Èç¹û±¾ÊéûÓÐÔĶÁÍ꣬ÏëÏ´μÌÐø½Ó×ÅÔĶÁ£¬¿ÉʹÓÃÉÏ·½ "Êղص½ÎÒµÄä¯ÀÀÆ÷" ¹¦ÄÜ ºÍ "¼ÓÈëÊéÇ©" ¹¦ÄÜ£¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡cannot¡¡instantiate¡¡an¡¡instance¡¡of¡¡the¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡following¡¡is¡¡the¡¡singleton¡¡code¡¡for¡¡ConfigurationLoader¡¡£¨placed¡¡in¡¡the¡¡Definitions¡¡¡¡
assembly£©¡£¡¡
Public¡¡Class¡¡ConfigurationLoader¡¡
¡¡¡¡¡¡¡¡Public¡¡Shared¡¡ReadOnly¡¡Property¡¡Instance£¨£©¡¡As¡¡ConfigurationLoader¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡ConfigurationLoader¡£_instance¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡Private¡¡Sub¡¡New£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_availableTypes¡¡=¡¡New¡¡Dictionary£¨Of¡¡String£»¡¡ConfigurationInfo£©£¨£©¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡Private¡¡Shared¡¡_instance¡¡As¡¡ConfigurationLoader¡¡
¡¡¡¡¡¡¡¡Shared¡¡Sub¡¡New£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_instance¡¡=¡¡New¡¡ConfigurationLoader£¨£©¡¡
¡¡¡¡¡¡¡¡End¡¡Sub¡¡
¡¡¡¡¡¡¡¡¡£¡¡¡£¡¡¡£¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡singleton¡¡property¡¡¡¡Instance¡¡is¡¡declared¡¡as¡¡Shared£»¡¡allowing¡¡a¡¡reference¡¡of¡¡¡¡
ConfigurationLoader¡£Instance¡£¡¡The¡¡implementation¡¡of¡¡the¡¡shared¡¡property¡¡must¡¡reference¡¡a¡¡¡¡
shared¡¡data¡¡member£»¡¡which¡¡is¡¡the¡¡data¡¡member¡¡_instance¡¡in¡¡this¡¡example¡£¡¡The¡¡data¡¡member¡¡¡¡
_instance¡¡is¡¡instantiated¡¡by¡¡the¡¡shared¡¡constructor¡¡Shared¡¡New£¨£©¡£¡¡The¡¡instance¡¡constructor¡¡will¡¡¡¡
instantiate¡¡the¡¡Dictionary¡¡_availableTypes¡£¡¡¡¡
¡öNote¡¡¡¡The¡¡shared¡¡constructor¡¡is¡¡called¡¡before¡¡the¡¡instance¡¡constructor¡¡and¡¡therefore¡¡before¡¡any¡¡client¡¡code¡¡¡¡
has¡¡a¡¡chance¡¡to¡¡access¡¡members¡¡of¡¡the¡¡class¡£¡¡
Using¡¡the¡¡Instantiated¡¡Type¡¡
Now¡¡that¡¡we¡¯ve¡¡covered¡¡the¡¡singleton¡¡aspect£»¡¡we¡¡need¡¡to¡¡get¡¡back¡¡to¡¡the¡¡original¡¡problem¡¡of¡¡¡¡
writing¡¡the¡¡code¡¡to¡¡instantiate¡¡a¡¡type¡¡dynamically¡£¡¡The¡¡following¡¡code¡¡is¡¡used¡¡to¡¡instantiate¡¡¡¡
Impl1¡¡in¡¡the¡¡Main£¨£©¡¡method¡¡of¡¡CallRuntimeImplementation¡¯s¡¡principal¡¡module¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡348¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
326¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ConfigurationLoader¡£Instance¡£Load£¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡definition¡¡as¡¡IDefinition¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ConfigurationLoader¡£Instance¡£Instantiate£¨Of¡¡IDefinition£©£¨¡¨Impl1¡¨£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Console¡£WriteLine£¨definition¡£TranslateWord£¨¡¨hello¡¨£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡You¡¡might¡¡get¡¡errors¡¡running¡¡the¡¡code¡£¡¡If¡¡you¡¡do£»¡¡it¡¡is¡¡because¡¡the¡¡assemblies¡¡are¡¡not¡¡in¡¡the¡¡same¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡path¡¡and¡¡the¡¡dynamic¡¡loading¡¡methods¡¡cannot¡¡find¡¡the¡¡assemblies¡£¡¡Solve¡¡this¡¡by¡¡copying¡¡all¡¡of¡¡the¡¡assemblies¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡into¡¡a¡¡single¡¡directory¡¡and¡¡running¡¡the¡¡code¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡first¡¡line¡¡is¡¡used¡¡to¡¡retrieve¡¡the¡¡assembly¡¡and¡¡type¡¡information¡¡from¡¡the¡¡configuration¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡file¡£¡¡The¡¡second¡¡line¡¡is¡¡used¡¡to¡¡instantiate¡¡Impl1¡¡and¡¡then¡¡cast¡¡¡¡Implementations1¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Implementation¡¡to¡¡type¡¡IDefinition¡£¡¡The¡¡cast¡¡will¡¡work¡¡because¡¡the¡¡IDefinition¡¡interface¡¡has¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡been¡¡implemented¡¡by¡¡Implementations1¡£Implementation¡£¡¡The¡¡assigned¡¡variable¡¡definition¡¡will¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡reference¡¡a¡¡valid¡¡object¡¡instance¡¡that¡¡implements¡¡IDefinition¡¡and¡¡the¡¡method¡¡TranslateWord£¨£©¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡At¡¡this¡¡point£»¡¡we¡¡need¡¡to¡¡step¡¡back¡¡and¡¡reflect¡¡on¡¡what¡¡has¡¡occurred¡£¡¡We¡¡have¡¡defined¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡within¡¡a¡¡configuration¡¡file¡¡the¡¡location¡¡of¡¡a¡¡type¡£¡¡Some¡¡source¡¡code¡¡in¡¡the¡¡application¡¡read¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡configuration¡¡file¡¡at¡¡runtime¡¡and¡¡stored¡¡the¡¡available¡¡types¡£¡¡The¡¡calling¡¡code¡¡knew¡¡about¡¡only¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡abstract¡¡type¡¡made¡¡available¡¡under¡¡the¡¡label¡¡Impl1¡¡and¡¡knew¡¡it¡¡implemented¡¡the¡¡interface¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡IDefinition¡£¡¡Then£»¡¡using¡¡some¡¡¡¡magic¡¡involving¡¡dynamic¡¡techniques£»¡¡the¡¡type¡¯s¡¡assembly¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡is¡¡loaded¡¡and¡¡instantiated¡£¡¡This¡¡process¡¡is¡¡unique¡¡because¡¡an¡¡administrator¡¡has¡¡the¡¡ability¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡update¡¡where¡¡and¡¡what¡¡type¡¡should¡¡be¡¡instantiated¡¡and¡¡called£»¡¡without¡¡needing¡¡to¡¡repile¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡or¡¡update¡¡the¡¡infrastructure¡£¡¡That¡¡flexibility¡¡makes¡¡it¡¡much¡¡simpler¡¡for¡¡you¡¡to¡¡update¡¡function
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ality¡¡without¡¡having¡¡to¡¡update¡¡the¡¡caller¡¡of¡¡the¡¡functionality¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡The¡¡ability¡¡to¡¡dynamically¡¡update¡¡the¡¡functionality¡¡is¡¡of¡¡benefit¡¡to¡¡the¡¡administrator¡¡of¡¡the¡¡application¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡There¡¡is¡¡minimal¡¡benefit¡¡to¡¡the¡¡developer£»¡¡because¡¡the¡¡developer¡¡has¡¡already¡¡converted¡¡the¡¡architecture¡¡to¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡use¡¡ponents¡£¡¡In¡¡general£»¡¡developers¡¡consider¡¡this¡¡approach¡¡to¡¡ponents¡¡as¡¡building¡¡a¡¡plug¡in¡¡architecture¡£¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Enhancing¡¡the¡¡Configuration¡¡File¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡configuration¡¡file¡¡is¡¡a¡¡bit¡¡of¡¡a¡¡clumsy¡¡definition¡¡because¡¡all¡¡of¡¡the¡¡available¡¡types¡¡are¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡stored¡¡in¡¡a¡¡single¡¡key/value¡¡pair¡£¡¡It¡¡would¡¡be¡¡preferable¡¡to¡¡be¡¡able¡¡to¡¡define¡¡custom¡¡items¡¡in¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡the¡¡configuration¡¡file£»¡¡as¡¡follows£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡approach¡¡is¡¡preferable¡¡because¡¡it¡¡is¡¡easier¡¡to¡¡read¡¡and¡¡understand£»¡¡thus¡¡reducing¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡chances¡¡that¡¡you¡¡will¡¡have¡¡an¡¡error¡£¡¡Additionally£»¡¡because¡¡the¡¡various¡¡identifiers¡¡are¡¡broken¡¡up£»¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡you¡¡don¡¯t¡¡need¡¡to¡¡build¡¡a¡¡lengthy¡¡single¡line¡¡string¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡349¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡327¡¡
¡öNote¡¡¡¡For¡¡simplicity£»¡¡we¡¡will¡¡have¡¡only¡¡a¡¡single¡¡dynamic¡¡type£»¡¡represented¡¡by¡¡the¡¡¡¡XML¡¡element¡£¡¡¡¡
In¡¡a¡¡plete¡¡solution£»¡¡you¡¡would¡¡use¡¡the¡¡same¡¡approach¡¡with¡¡different¡¡interfaces¡¡and¡¡types¡£¡¡
¡¡¡¡¡¡¡¡¡¡If¡¡you¡¡were¡¡to¡¡add¡¡the¡¡sample¡¡XML¡¡into¡¡the¡¡configuration¡¡file¡¡and¡¡run¡¡the¡¡¡¡application£»¡¡¡¡
you¡¡would¡¡get¡¡a¡¡configuration¡¡initialization¡¡failure¡£¡¡That¡¯s¡¡because¡¡the¡¡XML¡¡node¡¡¡¡is¡¡not¡¡a¡¡¡¡
standard¡¡configuration¡¡item¡¡and¡¡thus¡¡needs¡¡custom¡¡parsing¡£¡¡And¡¡when¡¡you¡¡want¡¡custom¡¡parsing£»¡¡¡¡
you¡¡need¡¡to¡¡tell¡¡the¡¡¡¡configuration¡¡infrastructure¡¡what¡¡code¡¡to¡¡execute¡£¡¡The¡¡following¡¡is¡¡¡¡
the¡¡plete¡¡¡¡configuration¡¡file¡¡with¡¡custom¡¡processing¡¡tags¡£¡¡
¡¡
¡¡
¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡
¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡
¡¡¡¡¡¡
¡¡
¡¡¡¡¡¡¡¡¡¡The¡¡bolded¡¡section¡¡represents¡¡a¡¡new¡¡part¡¡of¡¡the¡¡configuration¡¡file¡¡that¡¡could¡¡be¡¡called¡¡¡¡
¡°configuring¡¡the¡¡configuration¡£¡±¡¡Any¡¡XML¡¡node¡¡that¡¡is¡¡within¡¡the¡¡¡¡section¡¡is¡¡¡¡
used¡¡to¡¡customize¡¡the¡¡configuration¡parsing¡¡infrastructure¡£¡¡In¡¡the¡¡example£»¡¡the¡¡XML¡¡node¡¡¡¡
¡¡has¡¡two¡¡attributes¡¡that¡¡refer¡¡to¡¡what¡¡the¡¡configuration¡¡infrastructure¡¡should¡¡do¡¡¡¡
when¡¡it¡¡encounters¡¡a¡¡specific¡¡XML¡¡node¡£¡¡The¡¡attribute¡¡¡¡name¡¡specifies¡¡the¡¡name¡¡of¡¡the¡¡XML¡¡¡¡
node£»¡¡and¡¡the¡¡attribute¡¡type¡¡references¡¡a¡¡type¡¡£¨Definitions¡£LoaderSection£©¡¡and¡¡assembly¡¡¡¡
£¨Definitions£©¡¡that¡¡will¡¡process¡¡the¡¡XML¡¡node¡£¡¡
¡¡¡¡¡¡¡¡¡¡Step¡¡back¡¡and¡¡think¡¡about¡¡what¡¡is¡¡happening¡¡here¡£¡¡The¡¡XML¡¡node¡¡¡¡is¡¡used¡¡to¡¡¡¡
configure¡¡the¡¡configuration¡¡processor¡¡using¡¡dynamically¡¡loaded¡¡types¡£¡¡This¡¡demonstrates¡¡that¡¡¡¡
the¡¡process¡¡of¡¡dynamically¡¡loading¡¡and¡¡executing¡¡pieces¡¡of¡¡code¡¡is¡¡not¡¡rocket¡¡science£»¡¡but¡¡a¡¡¡¡
daily¡¡part¡¡of¡¡developing¡¡ponents¡¡in¡¡¡£¡¡
Marshaling¡¡the¡¡Configuration¡¡File¡¡Data¡¡
When¡¡you¡¡want¡¡to¡¡use¡¡a¡¡custom¡¡node¡¡in¡¡a¡¡configuration¡¡file£»¡¡you¡¡need¡¡to¡¡implement¡¡a¡¡class¡¡that¡¡¡¡
subclasses¡¡System¡£Configuration¡£ConfigurationSection¡£¡¡The¡¡purpose¡¡of¡¡the¡¡custom¡¡class¡¡is¡¡to¡¡¡¡
process¡¡and¡¡structure¡¡the¡¡data¡¡in¡¡the¡¡configuration¡¡file¡¡into¡¡something¡¡that¡¡the¡¡application¡¡can¡¡¡¡
manipulate¡£¡¡In¡¡other¡¡words£»¡¡we¡¡are¡¡marshaling¡¡the¡¡data¡¡from¡¡the¡¡configuration¡¡file¡¡to¡¡the¡¡¡¡¡¡
environment¡£¡¡Here¡¯s¡¡the¡¡start¡¡of¡¡our¡¡class£»¡¡which¡¡belongs¡¡in¡¡the¡¡Definitions¡¡assembly£º¡¡
Public¡¡Class¡¡LoaderSection¡¡
¡¡¡¡¡¡¡¡Inherits¡¡ConfigurationSection¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡350¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
328¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡¡¡infrastructure¡¡helps¡¡you¡¡implement¡¡the¡¡marshaling¡¡routines¡¡by¡¡providing¡¡class¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡types¡¡that¡¡allow¡¡you¡¡to¡¡explicitly¡¡associate¡¡an¡¡XML¡¡attribute¡¡or¡¡keyword¡¡with¡¡some¡¡variable¡£¡¡For¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡example£»¡¡suppose¡¡you¡¡want¡¡to¡¡cross¡reference¡¡the¡¡XML¡¡attribute¡¡easyname¡¡with¡¡a¡¡¡¡type¡£¡¡To¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡do¡¡so£»¡¡first¡¡define¡¡the¡¡nature¡¡and¡¡type¡¡of¡¡the¡¡property¡¡using¡¡the¡¡type¡¡ConfigurationProperty¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡£¨this¡¡code¡¡goes¡¡in¡¡the¡¡LoaderSection¡¡class£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Dim¡¡_propEasyName¡¡As¡¡ConfigurationProperty¡¡=¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡New¡¡ConfigurationProperty£¨¡¨easyname¡¨£»¡¡GetType£¨String£©£»¡¡Nothing£»¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ConfigurationPropertyOptions¡£IsRequired£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡This¡¡example¡¡uses¡¡the¡¡ConfigurationProperty¡¡constructor¡¡that¡¡has¡¡four¡¡parameters£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡¡¨easyname¡¨£º¡¡Identifies¡¡the¡¡XML¡¡attribute¡¡identifier¡¡that¡¡will¡¡be¡¡used¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡GetType£¨String£©£º¡¡Identifies¡¡the¡¡type¡¡of¡¡the¡¡attribute£»¡¡which¡¡could¡¡also¡¡include¡¡Double¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡and¡¡Integer¡¡to¡¡indicate¡¡a¡¡number¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡Nothing£º¡¡Identifies¡¡the¡¡default¡¡value¡¡of¡¡the¡¡attribute¡¡if¡¡the¡¡attribute¡¡is¡¡not¡¡present¡¡in¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡XML¡¡configuration¡¡file¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡o¡¡¡¡ConfigurationPropertyOptions¡£IsRequired£º¡¡Identifies¡¡some¡¡characteristics¡¡of¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡attribute£»¡¡in¡¡the¡¡case¡¡of¡¡this¡¡example£»¡¡the¡¡attribute¡¡is¡¡required¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡öNote¡¡¡¡There¡¡are¡¡multiple¡¡constructors¡¡for¡¡ConfigurationProperty¡£¡¡For¡¡more¡¡information£»¡¡check¡¡out¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡MSDN¡¡documentation¡¡£¨http£º//msdn2¡£microsoft¡£/en¡us/library/system¡£configuration¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡configurationproperty¡£configurationproperty¡£aspx£©¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Once¡¡you¡¡have¡¡defined¡¡a¡¡property£»¡¡you¡¡need¡¡to¡¡cross¡reference¡¡it¡¡with¡¡a¡¡property¡¡and¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡base¡¡class¡¡ConfigurationSection¡¡£¨again£»¡¡this¡¡code¡¡goes¡¡in¡¡the¡¡¡¡LoaderSection¡¡class£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡_¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Public¡¡ReadOnly¡¡Property¡¡EasyName£¨£©¡¡As¡¡String¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Get¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Return¡¡CStr£¨MyBase¡£Item£¨_propEasyName£©£©¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Get¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡End¡¡Property¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Whenever¡¡you¡¡reference¡¡the¡¡property¡¡EasyName£»¡¡the¡¡attributes¡¡associated¡¡with¡¡the¡¡property¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡provide¡¡a¡¡cross¡reference¡¡with¡¡the¡¡configuration¡¡file¡£¡¡Whenever¡¡the¡¡property¡¡is¡¡called£»¡¡the¡¡base¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡indexer¡¡with¡¡data¡¡member¡¡that¡¡references¡¡ConfigurationProperty¡¡is¡¡called¡¡and¡¡marshals¡¡the¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡data¡¡from¡¡the¡¡configuration¡¡file¡¡to¡¡the¡¡¡¡type¡£¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡The¡¡plete¡¡implementation¡¡used¡¡to¡¡process¡¡the¡¡XML¡¡node¡¡¡¡is¡¡as¡¡follows¡¡£¨added¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡to¡¡the¡¡Definitions¡¡assembly£©£º¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡Page¡¡351¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡
¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡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¡¡329¡¡
Impo
¿ì½Ý²Ù×÷: °´¼üÅÌÉÏ·½Ïò¼ü ¡û »ò ¡ú ¿É¿ìËÙÉÏÏ·ҳ °´¼üÅÌÉ쵀 Enter ¼ü¿É»Øµ½±¾ÊéĿ¼ҳ °´¼üÅÌÉÏ·½Ïò¼ü ¡ü ¿É»Øµ½±¾Ò³¶¥²¿!
ÎÂÜ°Ìáʾ£º ο´Ð¡ËµµÄͬʱ·¢±íÆÀÂÛ£¬Ëµ³ö×Ô¼ºµÄ¿´·¨ºÍÆäËüС»ï°éÃÇ·ÖÏíÒ²²»´íŶ£¡·¢±íÊéÆÀ»¹¿ÉÒÔ»ñµÃ»ý·ÖºÍ¾Ñé½±Àø£¬ÈÏÕæдԴ´ÊéÆÀ ±»²ÉÄÉΪ¾«ÆÀ¿ÉÒÔ»ñµÃ´óÁ¿½ð±Ò¡¢»ý·ÖºÍ¾Ñé½±ÀøŶ£¡