如果 MyApp.PluginsConfiguration 是 ConfigurationSection,那么您可以定义一个继承自 ConfigurationElementCollection 的新类,并使新类成为 MyApp.PluginsConfiguration 的 ConfigurationProperty
查看this article 了解有关这些类型的一些深入信息。我还 blogged about 具有嵌套属性,但不是专门用于集合。
编辑:这里有一些代码。给出的是 web.config 中的这一点:
<configSections>
<section name="plugins"
type="WebApplication1.PluginsConfiguration, WebApplication1"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll"/>
<use assembly="MyApp.Plugin2.dll"/>
</plugins>
这是实现这一目标的课程。请注意,可能需要处理 NullReferenceExceptions。
namespace WebApplication1
{
public class PluginsConfiguration : ConfigurationSection
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propPlugins;
static PluginsConfiguration()
{
propPlugins = new ConfigurationProperty(null, typeof(PluginsElementCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection);
properties = new ConfigurationPropertyCollection { propPlugins };
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElementCollection Plugins
{
get
{
return this[propPlugins] as PluginsElementCollection;
}
}
}
public class PluginsElementCollection : ConfigurationElementCollection
{
public PluginsElementCollection()
{
properties = new ConfigurationPropertyCollection();
}
private static ConfigurationPropertyCollection properties;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "use";
}
}
protected override ConfigurationElement CreateNewElement()
{
return new PluginsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
var elm = element as PluginsElement;
if (elm == null) throw new ArgumentNullException();
return elm.AssemblyName;
}
}
public class PluginsElement : ConfigurationElement
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propAssembly;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElement()
{
propAssembly = new ConfigurationProperty("assembly", typeof(string),
null,
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { propAssembly };
}
public PluginsElement(string assemblyName)
: this()
{
AssemblyName = assemblyName;
}
public string AssemblyName
{
get
{
return this[propAssembly] as string;
}
set
{
this[propAssembly] = value;
}
}
}
}
要访问它,这段代码 sn-p 应该会有所帮助:
var cfg = WebConfigurationManager.GetWebApplicationSection("plugins") as PluginsConfiguration;
var sb = new StringBuilder();
foreach(PluginsElement elem in cfg.Plugins)
{
sb.AppendFormat("{0}<br/>", elem.AssemblyName);
}
testLabel.Text = sb.ToString();
基本上我们有一个处理插件元素的配置部分。在此,我们指定一个 ConfigurationElementCollection 属性并将其声明为默认集合(理论上您可以在一个根节点下拥有多个不同的集合)。
PluginsElementCollection 实现了 ConfigurationElementCollection。 ElementName 必须是标签的名称,在我们的例子中是“use”。此外,GetElementKey 需要被覆盖,并且应该返回一个在条目中唯一的属性。
PluginsElement 然后实现单次使用标签。我们只定义了一个属性:AssemblyName,它映射到了assembly-attribute。
我并没有声称完全理解所有这些(特别是 ConfigurationElementCollection 以及它的各种 BaseAdd、BaseGet 等属性,这里并没有真正探索),但我可以声称这是可行的 :)
另外,它不使用任何属性。我讨厌属性——幸运的是,所有这些属性都可以转换为正确的代码。您可以使用其中一个(或两者)。