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