问题是,正如标题所说,每当我尝试从我的.config文件加载自定义CustomConfiguration部分时,我都会收到异常。我尝试加载的app.config文件看起来像这样。
....
<configSections>
<sectionGroup name="MainSection">
...
<section name="Directories" type="MyNamespace.DirectoriesSettings, Assembly"/>
</sectionGroup>
</configSections>
....
<MainSection>
...
<Directories Count="1">
<Directory id="1" Path="Some\Path" Working="And\Another\Path" Country="US"/>
</Directories>
</MainSection>
....
代码
namespace MyNamespace
{
internal class DirectoriesSettings : ConfigurationSection
{
[ConfigurationProperty("Count")]
public int Count { get { return Convert.ToInt32(base["Count"]); } }
[ConfigurationProperty("Directories", IsDefaultCollection = true, IsRequired = true)]
[ConfigurationCollection(typeof(DirectoryElement), AddItemName = "Directory", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public DirectoryElementCollection Directories { get { return (DirectoryElementCollection)base["Directories"]; } }
}
internal class DirectoryElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new DirectoryElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((DirectoryElement)element).Id;
}
}
internal class DirectoryElement : ConfigurationElement
{
[ConfigurationProperty("id", IsKey = true, IsRequired = true)]
public int Id
{
get { return Convert.ToInt32(this["id"]); }
set { this["id"] = value; }
}
[ConfigurationProperty("Path", IsKey = false, IsRequired = true)]
public string Path
{
get { return Convert.ToString(this["Path"]); }
set { this["Path"] = value; }
}
[ConfigurationProperty("Working", IsKey = false, IsRequired = true)]
public string Working
{
get { return Convert.ToString(this["Working"]); }
set { this["Working"] = value; }
}
[ConfigurationProperty("Country", IsKey = false, IsRequired = true)]
public string Country
{
get { return Convert.ToString(this["Country"]); }
set { this["Country"] = value; }
}
}
}
使用它的代码
class SettingsContainer
{
private const string PARENT_SECTION = "MainSection";
...
private DirectoriesSettings _directoriesSettings;
public DirectoriesSettings Directories { get { return _directoriesSettings; } }
...
public SettingsContainer()
{
...
_directoriesSettings = (DirectoriesSettings)ConfigurationManager.GetSection(string.Format("{0}/Directories", PARENT_SECTION));
...
}
}
每当我尝试设置_directoriesSettings变量时,都会抛出异常(无法识别的元素&#39;目录&#39;)。
我认为我做得对,但显然我错过了一些东西。
非常感谢任何帮助。
答案 0 :(得分:0)
<强>解决方案:强>
我发现了问题所在。
DirectoriesSettings类中的属性目录应该如下所示
[ConfigurationCollection(typeof(DirectoryElement), AddItemName = "Directory", CollectionType = ConfigurationElementCollectionType.BasicMap)]
[ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
public DirectoryElementCollection Directories { get { return (DirectoryElementCollection)this[""]; } }
而不是
[ConfigurationCollection(typeof(DirectoryElement), AddItemName = "Directory", CollectionType = ConfigurationElementCollectionType.BasicMap)]
[ConfigurationProperty("Directories", IsDefaultCollection = true, IsRequired = true)]
public DirectoryElementCollection Directories { get { return (DirectoryElementCollection)base["Directories"]; } }