按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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。