. NET System.Configuration namespaces provide the perfect support for customizing configurations in Web.config or app.config. Recently saw some projects in the custom XML file to do program configuration, so can not help to write a system to customize the configuration of the essay.
If you have a custom configuration at your fingertips, ignore this article.
To get to the point, let's take a look at one of the simplest custom configurations
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "Simple" type= "Configexample.configuration.simplesection,configexample"/>
</configSections>
<simple maxvalue= "minvalue=" 1 "></simple>
</configuration>
Using a custom configuration in a configuration file, you need to add a section element to the configsections and develop a corresponding type and name for this section element. The custom configuration is then added under the Configuration root node, as in the previous example, the simple node. Simple node only two integer attributes MaxValue and MinValue.
To use a custom configuration in a program we also need to implement the type of access to this configuration block, generally need to do the following three things:
1. Defining types from System.Configuration.ConfigurationSection inheritance
2. Define the properties of the configuration class that need to be decorated with the ConfigurationProperty attribute and set the name of the property in the configuration section and some other restriction information
3. Implement the property's get by using the string indexer of the base class, set
very simple and natural, the following is the implementation of the configuration class above:
Copy Code code as follows:
public class SimpleSection:System.Configuration.ConfigurationSection
{
[ConfigurationProperty ("MaxValue", Isrequired=false,defaultvalue=int32.maxvalue)]
public int MaxValue
{
Get
{
return (int) base["MaxValue"];
}
Set
{
base["MaxValue"] = value;
}
}
[ConfigurationProperty ("MinValue", isrequired=false,defaultvalue=1)]
public int MinValue
{
get {return (int) base["MinValue"];}
set {base["minvalue"] = value;}
}
[ConfigurationProperty ("Enabled", Isrequired=false,defaultvalue=true)]
public bool Enable
{
Get
{
return (BOOL) base["enabled"];
}
Set
{
base["Enabled"] = value;
}
}
}
In this way a simple configuration class is complete, how to use this configuration in the program? You need to use the ConfigurationManager class (to refer to System.configuration.dll This DLL is only available in the version after. Net2.0) to obtain the configuration of the GetSection method. The following code:
Copy Code code as follows:
Simplesection simple = configurationmanager.getsection ("simple") as simplesection;
Console.WriteLine ("Simple minvalue={0} maxValue = {1}", simple. Minvalue,simple. MaxValue);
This configuration class is too primitive, and maybe sometimes we need more complex constructs, such as using classes in the configuration class to represent a set of data, let's look at a slightly more complex custom configuration
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "complex" type= "Configexample.configuration.complexsection,configexample"/>
</configSections>
<complex height= "190" >
<child firstname= "James" lastname= "Bond"/>
</complex>
</configuration>
The name of this configuration is complex, he has an attribute height, and there is a child element in his node this element has two attributes FirstName and LastName; How do you implement this embedded node? First we need to define a class that inherits from the ConfigurationElement class and then uses a similar simplesection method to define some properties that are decorated with the ConfigurationProperty attribute, and of course the get of the attribute value, Set also uses the indexer of the base class. Like the following implementation:
Copy Code code as follows:
public class Complexsection:configurationsection
{
[ConfigurationProperty ("height", isrequired = True)]
public int Height
{
Get
{
return (int) base["height"];
}
Set
{
base["height"] = value;
}
}
[ConfigurationProperty ("child", Isdefaultcollection = False)]
Public Childsection Child
{
Get
{
Return (childsection) base[the "Child"];
}
Set
{
base["Child"] = value;
}
}
}
public class Childsection:configurationelement
{
[ConfigurationProperty ("FirstName", IsRequired = True, IsKey = True)]
public string FirstName
{
Get
{
Return (String) base["FirstName"];
}
Set
{
base["FirstName"] = value;
}
}
[ConfigurationProperty ("LastName", IsRequired = True)]
public string LastName
{
Get
{
Return (String) base["LastName"];
}
Set
{
base["LastName"] = value;
}
}
}
There is a slightly more complex scenario where we might configure a set of nodes of the same type in the configuration, that is, a set of nodes. As the following configuration:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "complex" type= "Configexample.configuration.complexsection,configexample"/>
</configSections>
<complex height= "190" >
<child firstname= "James" lastname= "Bond"/>
<children>
<add firstname= "Zhao" lastname= "Yukai"/>
<add firstname= "Lee" lastname= "Yukai"/>
<remove firstname= "Zhao"/>
</children>
</complex>
</configuration>
Look at the Children node, which is a collection class in which you define a set of add elements, or remove nodes to remove the configuration that has been added.
To use a custom node collection, you need to inherit a custom class from the ConfigurationElementCollection class, and then implement such Getelementkey (configurationelement element) and ConfigurationElement Createnewelement () two methods; for easy access to child nodes, you can define a read-only indexer within the class. Please see the following implementation
Copy Code code as follows:
public class Children:configurationelementcollection
{
protected override Object Getelementkey (configurationelement Element)
{
Return ((childsection) Element). FirstName;
}
protected override ConfigurationElement Createnewelement ()
{
return new Childsection ();
}
Public Childsection This[int I]
{
Get
{
Return (childsection) base. Baseget (i);
}
}
Public childsection this[string Key]
{
Get
{
Return (childsection) base. Baseget (key);
}
}
}
Of course, to use this collection class, we must add a property of this collection class to the complex class and specify attributes such as the element type of the collection class, as follows:
Copy Code code as follows:
[ConfigurationProperty ("Children", Isdefaultcollection = False)]
[Configurationcollection (typeof (Childsection), CollectionType = Configurationelementcollectiontype.addremoveclearmap, Removeitemname = "Remove")]
Public Children Children
{
Get
{
Return (Children) base["Children"];
}
Set
{
Base["Children"] = value;
}
}
We often use the construction of key-value pairs similar to the appsettings configuration section, when we don't have to do it ourselves, We can define a custom key-value pair directly using the existing System.Configuration.NameValueConfigurationCollection class. You can define the following properties in the complex class
Copy Code code as follows:
[ConfigurationProperty ("NVs", Isdefaultcollection = False)]
Public System.Configuration.NameValueConfigurationCollection NVs
{
Get
{
Return (namevalueconfigurationcollection) base["NVs"];
}
Set
{
base["NVs"] = value;
}
}
Then add the key-value pair configuration in the complex section of the configuration file
Copy Code code as follows:
<NVs>
<add name= "abc" value= "123"/>
<add name= "ABCD" value= "12d3"/>
</NVs>
This is basically enough to meet all the configuration requirements. But there is a bigger but less complicated concept, sectiongroup. We can customize sectiongroup and then configure multiple section in Sectiongroup, and grouping is meaningful for large applications.
The following configuration is configured with a sectiongroup containing simple and a complex two section
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<sectiongroup type= "Configexample.configuration.samplesectiongroup,configexample" name= "SampleGroup" >
<section type= "Configexample.configuration.simplesection,configexample" allowdefinition= "Everywhere" Simple "/>
<section type= "Configexample.configuration.complexsection,configexample" allowdefinition= "Everywhere" Complex "/>
</sectionGroup>
</configSections>
<sampleGroup>
<simple maxvalue= "minvalue=" 1 ">
</simple>
<complex height= "190" >
<child firstname= "James" lastname= "Bond"/>
<children>
<add firstname= "Zhao" lastname= "Yukai"/>
<add firstname= "Lee" lastname= "Yukai"/>
<remove firstname= "Zhao"/>
</children>
<NVs>
<add name= "abc" value= "123"/>
<add name= "ABCD" value= "12d3"/>
</NVs>
</complex>
</sampleGroup>
</configuration>
to facilitate access to the section in Sectiongroup, we can implement an inherited fromThe custom class for the System.Configuration.ConfigurationSectionGroup class. The implementation is simple by returning the section through the sections["sectionname" Indexer of the base class. As follows:
Copy Code code as follows:
public class SampleSectionGroup:System.Configuration.ConfigurationSectionGroup
{
Public Simplesection Simple
{
Get
{
Return (simplesection) base. sections["Simple"];
}
}
Public Complexsection Complex
{
Get
{
Return (complexsection) base. sections["Complex"];
}
}
}
It is to be noted that sectiongroup cannot be obtained using the Configurationmanager.getsection (string) method. To obtain sectiongroup must be obtained through the sectiongroups[string] indexer of the configuration class, the following sample code:
Copy Code code as follows:
Samplesectiongroup sample = (Samplesectiongroup) configurationmanager.openexeconfiguration ( Configurationuserlevel.none). sectiongroups["Samplegroup"];
Summary:
The. Net framework provides us with a very convenient configuration library, and we just need to inherit the simple configuration of the corresponding class to easily use the custom nodes that are configured in Web.config or app.config.
custom Profile Section source code