In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to add custom configuration in Web.config or App.config. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
The System.Configuration namespace in .net provides perfect support for customizing configurations in web.config or app.config. Recently, I have seen that some projects are still customizing the xml file to configure the program, so I can't help writing an essay about customizing the configuration with the system.
If you already know a lot about custom configuration, please ignore this article.
Back to the point, let's take a look at the simplest custom configuration.
The copy code is as follows:
To use a custom configuration in the configuration file, you need to add a section element to the configSections and specify the type and name for this section element. Then add this custom configuration under the configuration root node, such as the simple node in the example above. The simple node has 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, which generally requires the following three things:
1. Define types that inherit from System.Configuration.ConfigurationSection
two。 Define the properties of the configuration class, which need to be decorated with the ConfigurationProperty attribute, and specify the name of the property in the configuration section and some other restriction information
3. Get and set of attributes are implemented through the string indexer of the base class
Very simple and natural, the following is the implementation of the above configuration class:
The copy code is 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 completed, how to use this configuration in the program? You need to use the GetSection method of the ConfigurationManager class (to reference System.configuration.dll, a dll that is only available in versions after .net 2.0) to get the configuration. The code is as follows:
The copy code is as follows:
SimpleSection simple = ConfigurationManager.GetSection ("simple") as SimpleSection
Console.WriteLine ("simple minValue= {0} maxValue = {1}", simple.MinValue,simple.MaxValue)
This configuration class is so crude that sometimes we need more complex constructs, such as using classes to represent a set of data in the configuration class. Let's take a look at a slightly more complex custom configuration.
The copy code is as follows:
The name of this configuration is complex, which has an attribute height, and 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 define some properties modified with the ConfigurationProperty attribute in a way similar to SimpleSection. Of course, the get,set of the attribute value also uses the indexer of the base class. The implementation is as follows:
The copy code is 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 ["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
}
}
}
In a slightly more complex case, we may want to configure a set of nodes of the same type in the configuration, that is, a collection of nodes. Such as the following configuration:
The copy code is as follows:
Take a look at the children node, which is a collection class in which a set of add elements is defined, or there can be a remove node 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 () methods; read-only indexers can be defined in this class for easy access to child nodes. Please see the following implementation
The copy code is 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 an attribute of this collection class to the Complex class, and specify attributes such as the element type of the collection class, as follows:
The copy code is 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, so we no longer have to implement them ourselves, we can directly use the existing System.Configuration.NameValueConfigurationCollection class to define a custom key-value pair. The following properties can be defined in the Complex class
The copy code is 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 to the complex section of the configuration file
The copy code is as follows:
Basically, all the configuration requirements can be met here. But there is a larger but less complex concept, sectionGroup. It makes sense for large applications to customize SectionGroup and then configure multiple section; groups in sectionGroup.
In the following configuration, a sectionGroup containing simple and a complex section is configured
The copy code is as follows:
In order to easily access section in sectionGroup, we can implement a custom class that inherits from the System.Configuration.ConfigurationSectionGroup class. The implementation is simple by returning Section through the Sections ["sectionName"] indexer of the base class. As follows:
The copy code is 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 should be noted that SectionGroup cannot be obtained using the ConfigurationManager.GetSection (string) method, and sectionGroup must be obtained through the SectionGroups [string] indexer of the Configuration class, as shown in the following sample code:
The copy code is as follows:
SampleSectionGroup sample = (SampleSectionGroup) ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.None). SectionGroups ["sampleGroup"]
This is the end of this article on "how to solve the problem of adding custom configuration in Web.config or App.config". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.