.net 如何创建包含集合集合的配置节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10958054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Create a Configuration Section That Contains a Collection of Collections?
提问by John Saunders
I need a configuration section something like this:
我需要一个像这样的配置部分:
<myConfig>
<mySubConfig1>
<mySubSubConfig1 keyAttribute="value1">
<mySubSubConfig1Element keyAttribute="value1"/>
<mySubSubConfig1Element keyAttribute="value2"/>
</mySubSubConfig1>
<mySubSubConfig1 keyAttribute="value2">
<mySubSubConfig1Element keyAttribute="value1"/>
</mySubSubConfig1>
</mySubConfig1>
<mySubConfig2>
<mySubSubConfig2 keyAttribute="value1">
<mySubSubConfig2Element keyAttribute="value1"/>
<mySubSubConfig2Element keyAttribute="value2"/>
</mySubSubConfig2>
<mySubSubConfig2 keyAttribute="value2">
<mySubSubConfig2Element keyAttribute="value1"/>
</mySubSubConfig2>
</mySubConfig2>
<mySubConfig3>
<mySubSubConfig3 keyAttribute="value1">
<mySubSubConfig3Element keyAttribute="value1"/>
<mySubSubConfig3Element keyAttribute="value2"/>
</mySubSubConfig3>
<mySubSubConfig3 keyAttribute="value2">
<mySubSubConfig3Element keyAttribute="value1"/>
</mySubSubConfig3>
</mySubConfig3>
</myConfig>
I haven't yet found the magic that would permit this withoug using the old IConfigurationSectionHandlerinterface. Does anyone know how to do it?
我还没有找到允许使用旧IConfigurationSectionHandler界面进行此操作的魔法。有谁知道怎么做?
It's ok with me if myConfigand the mySubConfignare ConfigurationSectionGroupor ConfigurationSection.
if myConfigand the mySubConfignare ConfigurationSectionGroupor对我来说没问题ConfigurationSection。
Also, if it matters, this will be used from web.config.
此外,如果重要,这将从 web.config 使用。
回答by JDB still remembers Monica
In your example config file, myConfigwould be a class that inherits from ConfigurationSectionwith three properties named mySubConfig1, mySubConfig2and mySubConfig3.
在您的示例配置文件中,myConfig将是一个继承自ConfigurationSection三个名为mySubConfig1,mySubConfig2和 的属性的类mySubConfig3。
The type of the mySubConfig1property (as well as 2 and 3) would be a class that inherits from ConfigurationElementCollection, implements IEnumerable<ConfigElement>and is decorated with ConfigurationCollection(where the "AddItemName" property is set to "mySubSubConfig1").
mySubConfig1属性的类型(以及 2 和 3)将是一个继承自ConfigurationElementCollection、实现IEnumerable<ConfigElement>和修饰的类ConfigurationCollection(其中“AddItemName”属性设置为“mySubSubConfig1”)。
Below is a complete sample implementation of an approach I used in a production deployment. Be sure to include the System.Configuration assembly. (It's a bit confusing because the System.Configuration namespace is defined in other assmeblies, but you must include the System.Configuration assemblyto use the code below.)
下面是我在生产部署中使用的方法的完整示例实现。确保包含 System.Configuration程序集。(这有点令人困惑,因为 System.Configuration 命名空间是在其他程序集中定义的,但您必须包含 System.Configuration程序集才能使用下面的代码。)
Here are the custom configuration classes:
以下是自定义配置类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ConfigTest {
class CustomConfigSection : ConfigurationSection {
[ConfigurationProperty( "ConfigElements", IsRequired = true )]
public ConfigElementsCollection ConfigElements {
get {
return base["ConfigElements"] as ConfigElementsCollection;
}
}
}
[ConfigurationCollection( typeof( ConfigElement ), AddItemName = "ConfigElement" )]
class ConfigElementsCollection : ConfigurationElementCollection, IEnumerable<ConfigElement> {
protected override ConfigurationElement CreateNewElement() {
return new ConfigElement();
}
protected override object GetElementKey( ConfigurationElement element ) {
var l_configElement = element as ConfigElement;
if ( l_configElement != null )
return l_configElement.Key;
else
return null;
}
public ConfigElement this[int index] {
get {
return BaseGet( index ) as ConfigElement;
}
}
#region IEnumerable<ConfigElement> Members
IEnumerator<ConfigElement> IEnumerable<ConfigElement>.GetEnumerator() {
return ( from i in Enumerable.Range( 0, this.Count )
select this[i] )
.GetEnumerator();
}
#endregion
}
class ConfigElement : ConfigurationElement {
[ConfigurationProperty( "key", IsKey = true, IsRequired = true )]
public string Key {
get {
return base["key"] as string;
}
set {
base["key"] = value;
}
}
[ConfigurationProperty( "SubElements" )]
public ConfigSubElementsCollection SubElements {
get {
return base["SubElements"] as ConfigSubElementsCollection;
}
}
}
[ConfigurationCollection( typeof( ConfigSubElement ), AddItemName = "ConfigSubElement" )]
class ConfigSubElementsCollection : ConfigurationElementCollection, IEnumerable<ConfigSubElement> {
protected override ConfigurationElement CreateNewElement() {
return new ConfigSubElement();
}
protected override object GetElementKey( ConfigurationElement element ) {
var l_configElement = element as ConfigSubElement;
if ( l_configElement != null )
return l_configElement.Key;
else
return null;
}
public ConfigSubElement this[int index] {
get {
return BaseGet( index ) as ConfigSubElement;
}
}
#region IEnumerable<ConfigSubElement> Members
IEnumerator<ConfigSubElement> IEnumerable<ConfigSubElement>.GetEnumerator() {
return ( from i in Enumerable.Range( 0, this.Count )
select this[i] )
.GetEnumerator();
}
#endregion
}
class ConfigSubElement : ConfigurationElement {
[ConfigurationProperty( "key", IsKey = true, IsRequired = true )]
public string Key {
get {
return base["key"] as string;
}
set {
base["key"] = value;
}
}
}
}
Here's the App.config file:
这是 App.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomConfigSection" type="ConfigTest.CustomConfigSection,ConfigTest" />
</configSections>
<CustomConfigSection>
<ConfigElements>
<ConfigElement key="Test1">
<SubElements>
<ConfigSubElement key="-SubTest1.1" />
<ConfigSubElement key="-SubTest1.2" />
</SubElements>
</ConfigElement>
<ConfigElement key="Test2">
<SubElements>
<ConfigSubElement key="-SubTest2.1" />
<ConfigSubElement key="-SubTest2.2" />
</SubElements>
</ConfigElement>
</ConfigElements>
</CustomConfigSection>
</configuration>
Finally, here's the code which accesses and uses the config file:
最后,这是访问和使用配置文件的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace ConfigTest {
class Program {
static void Main( string[] args ) {
var l_configSettings = (CustomConfigSection) ConfigurationManager.GetSection( "CustomConfigSection" );
foreach ( var l_element in l_configSettings.ConfigElements.AsEnumerable() ) {
Console.WriteLine( l_element.Key );
foreach ( var l_subElement in l_element.SubElements.AsEnumerable() ) {
Console.WriteLine( l_subElement.Key );
}
}
Console.WriteLine( "Press any key..." );
Console.ReadKey( true );
}
}
}
A lighter-weight alternative was written by Sunil Singh on his blog:
http://blogs.quovantis.com/net-creating-a-custom-configuration-section-that-contains-a-collection-of-collections/
Sunil Singh 在他的博客上写了一个轻量级的替代方案:http:
//blogs.quovantis.com/net-creating-a-custom-configuration-section-that-contains-a-collection-of-collections/

