友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
一世书城 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第117章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!



e 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 



Imports System。Configuration 



Public Class LoaderSection 

    Inherits ConfigurationSection 



    Private Shared _propAssemblyName As ConfigurationProperty = _ 

      New ConfigurationProperty(〃assemblyname〃; GetType(String); Nothing; _ 

                                ConfigurationPropertyOptions。IsRequired) 

    Private Shared _propEasyName As ConfigurationProperty = _ 

      New ConfigurationProperty(〃easyname〃; GetType(String); Nothing; _ 

                                ConfigurationPropertyOptions。IsRequired) 

    Private Shared _properties As ConfigurationPropertyCollection = _ 

      New ConfigurationPropertyCollection() 

    Private Shared _propTypeName As ConfigurationProperty = _ 

      New ConfigurationProperty(〃typename〃; GetType(String); Nothing; _ 

                                ConfigurationPropertyOptions。IsRequired) 



    Shared Sub New() 

        _properties。Add(_propAssemblyName) 

        _properties。Add(_propEasyName) 

        _properties。Add(_propT
返回目录 上一页 下一页 回到顶部 0 1
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!