大家好,我想在项目中实现自定义配置部分。但是有些我不明白的地方所以行不通。
我有看起来像这样的App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DepartmentConfigurationSection" type="Statistics.Config.DepartmentSection , Program1"/>
</configSections>
<s>
<Cash>
<add Number="1" Name="Money" />
</Cash>
<Departments>
<add Id="1" Name="x" />
<add Id="2" Name="y" />
</Departments>
</s>
</configuration>
我创建了一个名为DepartmentSection.cs的文件,其中包含ConfigurationElement,ConfigurationElementCollection和ConfigurationSection。该类是这样的:
public class DepartmentConfig : ConfigurationElement
{
public DepartmentConfig() { }
public DepartmentConfig(int id, string name)
{
Id = id;
Name = name;
}
[ConfigurationProperty("Id", IsRequired = true, IsKey = true)]
public int Id
{
get { return (int)this["Id"]; }
set { this["Id"] = value; }
}
[ConfigurationProperty("Name", IsRequired = true, IsKey = false)]
public string Name
{
get { return (string)this["Name"]; }
set { this["Name"] = value; }
}
}
public class DepartmentCollection : ConfigurationElementCollection
{
public DepartmentCollection()
{
Console.WriteLine("ServiceCollection Constructor");
}
public DepartmentConfig this[int index]
{
get { return (DepartmentConfig)BaseGet(index); }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public void Add(DepartmentConfig depConfig)
{
BaseAdd(depConfig);
}
public void Clear()
{
BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new DepartmentConfig();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((DepartmentConfig)element).Id;
}
public void Remove(DepartmentConfig depConfig)
{
BaseRemove(depConfig.Id);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
}
public class DepartmentConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Departments", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(DepartmentCollection),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public DepartmentCollection Departments
{
get
{
return (DepartmentCollection)base["Departments"];
}
}
}
我试图从处理程序中获取集合,但是没有成功。我尝试这样,但是给了我这个错误:“无法初始化系统配置”。
DepartmentConfigurationSection serviceConfigSection =
ConfigurationManager.GetSection("s") as DepartmentConfigurationSection;
DepartmentConfig serviceConfig = serviceConfigSection.Departments[0];
请帮帮我!谢谢..
问题似乎出在您app.config
(或web.config
)。包含自定义配置XML的元素必须与在中的name
属性中指定的名称匹配configSections\section
。例如,为了使代码按编写的方式工作,app.config应该看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="s" type="Statistics.Config.DepartmentConfigurationSection, Program1"/>
</configSections>
<s>
<Cash>
<add Number="1" Name="Money" />
</Cash>
<Departments>
<add Id="1" Name="x" />
<add Id="2" Name="y" />
</Departments>
</s>
</configuration>
如您所见,section name="s"
匹配项与s
元素的名称匹配。另外,您将类型列为Statistics.Config.DeptartmentSection
,但类名是DepartmentConfigurationSection
,因此它应与您尝试加载的类匹配。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句