在自定义ConfigurationSection中出现无法识别的元素异常
本文关键字:识别 元素 异常 自定义 ConfigurationSection | 更新日期: 2023-09-27 18:07:33
问题是,正如标题所说,每当我试图从我的.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));
...
}
}
当我试图设置_directoressettings变量时,抛出异常(无法识别的元素'Directory')。
我认为我做得对,但显然我遗漏了一些东西。
解决方案:
我找到问题所在了。
directoressettings类中的目录属性应该如下所示
[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"]; } }