您应该创建自己的ConfigurationSection
定义,而不是依赖于让您的值落在字符串拆分操作的正确索引处。
请参阅MSDN 上的 How To和MSDN ConfigurationProperty 示例。
以下是一些帮助您入门的代码:
class CustomConfig : ConfigurationSection
{
private readonly CustomElementCollection entries =
new CustomElementCollection();
[ConfigurationProperty("customEntries", IsDefaultCollection = true)]
[ConfigurationCollection(typeof(CustomElementCollection), AddItemName = "add")]
public CustomElementCollection CustomEntries { get { return entries; } }
}
class CustomElementCollection : ConfigurationElementCollection
{
public CustomElement this[int index]
{
get { return (CustomElement) BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new CustomElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CustomElement)element).Name;
}
}
class CustomElement : ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get { return this["name"] as string; }
set { this["name"] = value; }
}
[ConfigurationProperty("direction", IsRequired = true)]
public string Direction
{
get { return this["direction"] as string; }
set { this["direction"] = value; }
}
[ConfigurationProperty("filePath", IsRequired = true)]
public string FilePath
{
get { return this["filePath"] as string; }
set { this["filePath"] = value; }
}
}
指定自定义配置后,您可以Select
使用自定义中指定的任何属性使用 lambda ConfigurationElement
。