我需要创建一个简单的自定义 ConfigurationSection,其中包含一个 IEnumerable。
我已经阅读了几篇文章和stackoverflow链接,以此为例: 如何在app.config中创建自定义配置部分?
所以,我在控制台应用程序中有这个配置文件部分:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="Disk"
type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection"/>
</configSections>
<Disk>
<Paths>
<Path name="one" permission="1" />
<Path name="two" permission="2" />
<Path name="three" permission="3" />
</Paths>
</Disk>
</configuration>
接下来我有整个结构来管理配置部分: using System.Configuration;
namespace ConsoleApplication1_ConfigurationEnumerable
{
public class Path: ConfigurationElement
{
[ConfigurationProperty("name", IsRequired = true)]
public string Name
{
get
{
return (string)this["name"];
}
}
[ConfigurationProperty("permission", IsRequired=true)]
public string Permission
{
get
{
return (string)this["permission"];
}
}
}
public class Paths: ConfigurationElementCollection
{
public Path this[int index]
{
get
{
return base.BaseGet(index) as Path;
}
set
{
if (base.BaseGet(index) != null) {
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Path();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Path)element).Name;
}
}
public class PathsConfigSection : ConfigurationSection
{
public static PathsConfigSection GetConfig()
{
//return (PathsConfigSection)System.Configuration.ConfigurationManager.GetSection("Disk") ?? new PathsConfigSection();
return (PathsConfigSection)System.Configuration.ConfigurationManager.GetSection("Paths") ?? new PathsConfigSection();
}
[ConfigurationProperty("Paths")]
public Paths Paths
{
get
{
object o = this["Paths"];
return o as Paths;
}
}
}
}
在这里,program.cs 使用了整个东西: using System;
namespace ConsoleApplication1_ConfigurationEnumerable
{
class Program
{
static void Main(string[] args)
{
var config = PathsConfigSection.GetConfig();
if (config == null || config.Paths.Count == 0)
{
Console.WriteLine("Is null or empty");
}
else
{
foreach (Path item in config.Paths)
{
Console.WriteLine("Item {0} with valuer {1}", item.Name, item.Permission);
}
}
}
}
}
这里的问题在于这两行:
//return (PathsConfigSection)System.Configuration
// .ConfigurationManager.GetSection("Disk") ?? new PathsConfigSection();
return (PathsConfigSection)System.Configuration
.ConfigurationManager.GetSection("Paths") ?? new PathsConfigSection();
如果我使用第二个(上面未注释),它将返回 null。
如果我使用注释过的,那么它会引发如下异常:
System.Configuration.ConfigurationErrorsException 未处理
HResult=-2146232062 消息=为磁盘创建配置节处理程序时出错:无法从程序集“System.Configuration,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”加载类型“ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection”。(C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config 第 4 行) Source=System.Configuration BareMessage=为磁盘创建配置节处理程序时出错:无法加载类型来自程序集“System.Configuration,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a”的“ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection”。
ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System。 System.Threading.ThreadHelper.ThreadStart() InnerException 处的 Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态):System.TypeLoadException HResult=-2146233054 消息=无法从程序集“System.配置,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a'。源=系统。
我的错在哪里?