C # create custom configuration section

Source: Internet
Author: User

Applications in. netProgramIn, we often see that the project vs generates for us contains files such as app. config or web. config. This file is what we call the application configuration file. This file describes some information related to our applications, such as database connection and Authentication mode. The connectionstrings attribute of configurationmanager can be used in the program to conveniently obtain the database connection string information in the configuration file.

However, sometimes we need to expand it and add some custom elements, instead of simply using the default configuration. For example, we may need to dynamically load a class at program startup and initialize it. However, we do not know this class or initialization data during program design. I believe everyone has encountered such a problem, so I will not explain it too much here. The best way is to write these things that may change into the configuration file. When you are certain, you only need to modify the configuration file, instead of enabling vs to change it in 30 seconds when the product is launched immediatelyCode.

It is easy to add some custom elements to the configuration file. You only need to do so in two steps:

1. register the node name you want to define and the configuration section handler for processing the node in the <configsections> node. The Code is as follows:

1 <configsections>

2 <section name = "dbfactory" type = "dbfactory. configuration. dbfactorysection, dbfactory. Configuration"/>

3 </configsections>

2. Add custom nodes in the appropriate positions. The Code is as follows:

1 <configsections>

2 <section name = "dbfactory" type = "dbfactory. configuration. dbfactorysection, dbfactory. Configuration"/>

3 </configsections>

4 <dbfactory>

5 <default factory = "SQL"> </default>

6 <factorys>

7 <Add name = "SQL" assembly = "Hello. Data" class = "sqldbfactory"/>

8 <Add name = "oracle" assembly = "Hello. Data" class = "oracledbfactory"/>

9 </factorys>

10 </dbfactory>

The custom node is added, but how can we get the configuration information in the program? I believe that many of you are masters of XML, using system. some Classes in XML write an XML parsing class and parse the custom node information. Will we get what we want? This is indeed the case. Daniel is really a great guy, and he really admire me. However, if this method is used, the younger brother does not need to write this blog post.

The. NET Framework implements a lot of configuration APIs for us to simplify the operations on the configuration file. In the first step, we mentioned the configuration section handler, which is used to read and write custom node information. How can we implement a configuration section handler? First, let's take a look at the related classes and concepts.

Configurationsection: all custom nodes must inherit this class to provide custom processing and programming access to the custom configuration section.

Configurationelement: an element in the configuration file.

Configurationelementcollection: a configuration element that contains a collection of child elements.

With these classes, we can conclude that there are two types of configuration elements in the configuration file.

First, a single configuration element, that is, the element inherited from the configurationelement, does not contain any child element.

Second, the collection configuration element inherits from the configurationelementcollection element, which contains a collection of child elements.

The concepts are often abstract. To understand these things, we should give a detailed description based on the examples given above.

 

In the <dbfactory> Configuration section, there are two elements: <default> and <factorys>, and the <factorys> element contains two child elements. Here, <default> is a single configuration element, and <factorys> is a collection configuration element. We need to implement the classes and their attributes corresponding to these elements respectively.

<Default> element: it is a single element, so we inherit the configurationelement. This element has a factory attribute, So we define it in the class. The Code is as follows:

Code

Public class defaultelement: configurationelement

{

[Configurationproperty ("Factory")]

Public string factory

{

Get

{

Return this ["Factory"] as string;

}

Set

{

This ["Factory"] = value;

}

}

}

Note: We need to register the configurationproperty feature of the property above the property definition.

<Factorys> child element:

Code

Public class factoryelement: configurationelement

{

[Configurationproperty ("name")]

Public string name

{

Get

{

Return this ["name"] as string;

}

Set

{

This ["name"] = value;

}

}

[Configurationproperty ("assembly")]

Public String Assembly

{

Get

{

Return this ["assembly"] as string;

}

Set

{

This ["assembly"] = value;

}

}

[Configurationproperty ("class")]

Public string class

{

Get

{

Return this ["class"] as string;

}

Set

{

This ["class"] = value;

}

}

}

<Factorys> an element is a collection element that inherits configurationelementcollection.

Code

Public class factoryelements: configurationelementcollection

{

Protected override configurationelement createnewelement ()

{

Return new factoryelement ();

}

Protected override object getelementkey (configurationelement element)

{

Return (factoryelement) element). Name;

}

Public factoryelement this [string name]

{

Get

{

Return baseget (name) as factoryelement;

}

}

}

The configurationelementcollection class is an abstract class. You should display the createnewelement method and getelementkey method to implement it.

<Dbfactory> node, inherited from configurationsection

Code

Public class dbfactorysection: configurationsection

{

[Configurationproperty ("default")]

Public defaultelement defaultfactory

{

Get

{

Return this ["default"] As defaultelement;

}

Set

{

This ["default"] = value;

}

}

[Configurationproperty ("factorys")]

Public factoryelements factorys

{

Get

{

Return this ["factorys"] As factoryelements;

}

Set

{

This ["factorys"] = value;

}

}

}

The configuration section handler is finally written. Put these four classes in the same project directory and compile them into a DLL. Reference This dll where you need to obtain the configuration information. Use dbfactorysection section = configurationmanager. getsection ("dbfactory") as dbfactorysection. Try again. Isn't section what you want?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.