小牛电子书 > 其他电子书 > VB2008从入门到精通(PDF格式英文版) >

第118章

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

小说: VB2008从入门到精通(PDF格式英文版) 字数: 每页3500字

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






    Shared Sub New() 

        _properties。Add(_propAssemblyName) 

        _properties。Add(_propEasyName) 

        _properties。Add(_propTypeName) 

    End Sub 



     _ 

    Public ReadOnly Property AssemblyName() As String 

        Get 

            Return CStr(MyBase。Item(_propAssemblyName)) 

        End Get 

    End Property 



     _ 

    Public ReadOnly Property EasyName() As String 

        Get 

            Return CStr(MyBase。Item(_propEasyName)) 

        End Get 

    End Property 



     _ 

    Public ReadOnly Property TypeName() As String 

        Get 

            Return CStr(MyBase。Item(_propTypeName)) 

        End Get 

    End Property 

End Class 


…………………………………………………………Page 352……………………………………………………………

330       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  LoaderSection class does two jobs: it tells the underlying configuration infrastructure  

          what  LoaderSection is interested in; and it provides an easy…to…use API to the configuration  

           data。 The first job is nice to have done; but the second is absolutely essential。 

                To understand what happens; let’s see an example of retrieving the values from a configu

          ration file。  



           Reading the Enhanced Configuration File 



          As you’ve seen; the  element declares a custom XML node that; when encoun

          tered; instantiates a specific type— LoaderSection is instantiated in this example。  

                The values are retrieved from the custom configuration as follows: 



               o  The configuration infrastructure reads the various attributes; such as  

                  。 



               o  The configuration infrastructure does a cross…reference of which attributes exist and  

                  if they are required。 Think of this step as the bookkeeping of the custom configuration  

                  information。 



               o  The configuration infrastructure waits for the application code to call the properties  

                  EasyName; TypeName; or AssemblyName。 In the implementation of the individual properties;  

                  the MyBase indexer (such as MyBase。Item(_propTypeName) from  LoaderSection。TypeName)  

                  is referenced; and the value of the configuration item is retrieved using the property  

                  descriptors declared as shared members。 



                We can use the following code to read the custom configuration item (added to the  

           Definitions assembly)。 This code is an example of replacing the previously declared  Load()  

          functionality。 



           Public Sub Load()  

              Dim loader As LoaderSection = _ 

                CType(ConfigurationManager。GetSection(〃loader〃); LoaderSection) 

              If loader IsNot Nothing Then 

                  Dim configInfo As ConfigurationInfo = _ 

                    New ConfigurationInfo() With { _ 

                                        。EasyName = loader。EasyName; _ 

                                        。TypeName = loader。TypeName; _ 

                                        。AssemblyName = loader。AssemblyName} 



                  _availableTypes。Add(loader。EasyName; configInfo) 



              End If 

           End Sub 



                The only major difference between this code and the previous  Load() code is the  

           ConfigurationManager reference。 In the previous  Load() implementation; it was assumed that  

           all configuration settings were stored in the  section。 This time; we retrieve the  

            section; which is defined to be the type  LoaderSection。 We still create all available  

          types using the _availableTypes dictionary collection; but the parsing and breaking apart of  

          the various buffers are not necessary。 


…………………………………………………………Page 353……………………………………………………………

           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 331 



■Note  Writing your own configuration section is not that difficult; but it is tedious。 It is tedious because you  

need to explicitly tell the configuration infrastructure what every item means and how you will use it。 There is  

no simple way to get around this; because the configuration infrastructure needs to know which configuration  

items should be processed and how they should be processed。 Thus; to implement a configuration section  

with multiple items; you will need to use more pieces of the predefined configuration infrastructure in the  

same manner as illustrated by this simple example。 See the  configuration infrastructure documentation;  

at http://support。microsoft。/kb/313405/EN…US/; for more information。 



Loading a Strongly Named Assembly 



In the previous section; the example demonstrated the dynamic loading technique in which  

you explicitly define where the assembly is and then instantiate a type in that assembly。 Another  

approach to instantiating an assembly involves using the formal declaration of a type。 

      In ; there is an easy way to reference an assembly and a more difficult way to reference an  

assembly。 This is analogous to the easy way to identify a person and the more plicated way。  

The easy way to identify me is to use my name; Christian Gross; and that works to a degree。 The  

problem is that I’m not the only Christian Gross on this planet。 The precise way to find me is to  

look at my passport。 The passport approach works; but it has a long; ugly number that is hard  

to remember。 

      The assembly name Implementations2。dll is a simple assembly identifier like Christian Gross。  

The following is the precise  identifier of the assembly。 



Implementations2; Version=1。0。0。0; Culture=neutral; PublicKeyToken=6bc4c8c27c08ba48 



      The identifier is quite a mouthful; but it says everything you need to know。 This specifies  

the name as Implementations2; the version number; the culture (bination of language and  

country); and a fixed…signed identifier。 When culture is defined as neutral; it means that only  

the language is taken into account and not the country…specific bit。 For example; if the culture  

is en…CA; with neutral; the CA bit will be ignored; which means a neutral form of English; known  

in this context as international English; will be used。  

      Using all of these attributes; you can uniquely identify an assembly。 This lets you load a  

specific version of the assembly; allowing multiple similar assemblies to be loaded simulta

neously。 This specific referencing lets you to use ponents according to version and language。 

      The extra information is specified in the source code file AssemblyInfo。vb。 If you want to edit  

this file; select the project and then choose Project  Show All Files。 You’ll find the AssemblyInfo。vb  

file under the My Project node in the Solution Explorer。 Following is the plete implementation  

for Implementations2。dll。 



Imports System 

Imports System。Reflection 

Imports System。Runtime。InteropServices 



 ' General Information about an assembly is controlled through the following  

 ' set of attributes。 Change these attribute values to modify the information 

 ' associated with an assembly。 


…………………………………………………………Page 354……………………………………………………………

332       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 



          ' Review the values of the assembly attributes 



          

返回目录 上一页 下一页 回到顶部 2 2

你可能喜欢的