ConfigurationSection中的自定义IEnumerable [英] Customized IEnumerable in ConfigurationSection
问题描述
我需要创建一个简单的自定义ConfigurationSection,其中包含IEnumerable.
I need to create a simple customized ConfigurationSection with an IEnumerable inside it.
我已阅读了几篇文章和stackoverflow链接,将其作为一个简单示例: 如何在app.config中创建自定义配置部分?
I have read several articles and stackoverflow links, taking this as a simple example: How to create custom config section in app.config?
因此,我在控制台应用程序中有此配置文件部分:
So, I have this config file section inside a Console App:
<?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>
接下来,我将使用整个结构来管理config部分: 使用System.Configuration;
Next I have this whole structure to manage the config section: 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: 使用系统;
And here the program.cs using the whole thing: 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);
}
}
}
}
}
这里的问题在这两行之内:
The problem here is inside this two lines:
//return (PathsConfigSection)System.Configuration
// .ConfigurationManager.GetSection("Disk") ?? new PathsConfigSection();
return (PathsConfigSection)System.Configuration
.ConfigurationManager.GetSection("Paths") ?? new PathsConfigSection();
如果我使用第二个(上面未注释),它将返回null.
If I use the second one (uncommented above) it returns null.
如果我使用注释的,则会引发如下异常:
If I use the commented one, then it throws an exception like this:
未处理System.Configuration.ConfigurationErrorsException
HResult = -2146232062消息=创建 磁盘的配置部分处理程序:无法加载类型 来自的"ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection" 程序集'System.Configuration,Version = 4.0.0.0,Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'. (C:\ Users \ blackberry \ Desktop \ ConsoleApplication1_ConfigurationEnumerable \ ConsoleApplication1_ConfigurationEnumerable \ bin \ Debug \ ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config 第4行)Source = System.Configuration BareMessage =发生错误 为磁盘创建配置节处理程序:无法加载 类型'ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection' 来自程序集'System.Configuration,Version = 4.0.0.0,Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'.
文件名= C:\ Users \ blackberry \ Desktop \ ConsoleApplication1_ConfigurationEnumerable \ ConsoleApplication1_ConfigurationEnumerable \ bin \ Debug \ ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config 行= 4 StackTrace: 在System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey,布尔值和isRootDeclaredHere) 在System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey,布尔值getLkg,布尔值checkPermission,布尔值 getRuntimeObject,布尔型请求IsHere,Object&结果,对象与对象 resultRuntimeObject) 在System.Configuration.BaseConfigurationRecord.GetSection(String configKey) 在System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) 在System.Configuration.ConfigurationManager.GetSection(String sectionName) 在ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection.GetConfig() 在 C:\ Users \ blackberry \ Desktop \ ConsoleApplication1_ConfigurationEnumerable \ ConsoleApplication1_ConfigurationEnumerable \ Disk.cs:line 63 在ConsoleApplication1_ConfigurationEnumerable.Program.Main(String [] args)在 C:\ Users \ blackberry \ Desktop \ ConsoleApplication1_ConfigurationEnumerable \ ConsoleApplication1_ConfigurationEnumerable \ Program.cs:line 9 在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String []参数) 在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)中 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executeContext,ContextCallback回调,对象状态,布尔值 reserveSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext执行上下文,ContextCallback回调,对象状态) 在System.Threading.ThreadHelper.ThreadStart()处InnerException:System.TypeLoadException HResult = -2146233054 消息=无法从中加载类型'ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection' 程序集'System.Configuration,Version = 4.0.0.0,Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'. 源= System.Configuration TypeName = ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection 堆栈跟踪: 在System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost 主机,字符串类型字符串,布尔型throwOnError) 在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord) 在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord) 在System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord) 在System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey,布尔值和isRootDeclaredHere) InnerException:
System.Configuration.ConfigurationErrorsException was unhandled
HResult=-2146232062 Message=An error occurred creating the configuration section handler for Disk: Could not load type 'ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config line 4) Source=System.Configuration BareMessage=An error occurred creating the configuration section handler for Disk: Could not load type 'ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Filename=C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config Line=4 StackTrace: at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject) at System.Configuration.BaseConfigurationRecord.GetSection(String configKey) at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection.GetConfig() in C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\Disk.cs:line 63 at ConsoleApplication1_ConfigurationEnumerable.Program.Main(String[] args) in C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\Program.cs:line 9 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.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.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.TypeLoadException HResult=-2146233054 Message=Could not load type 'ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Source=System.Configuration TypeName=ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection StackTrace: at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord) at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord) at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere) InnerException:
我的错在哪里?
推荐答案
您需要使用configSections标记中指定类时,nofollow>完全合格的程序集名称:
You need to use the Fully Qualified Assembly Name when specifying the class in the configSections
tag:
<configSections>
<section name="Disk"
type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection, ConsoleApplication1"/>
</configSections>
这假定您的程序集的名称为ConsoleApplication1
.如果仍然遇到异常,则可以使用以下代码确定正确的值:
This assumes that the name of your assembly is ConsoleApplication1
. If you are still getting exceptions, you can determine the correct value using the the following code:
typeof(ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection).FullName
.ToString()
顺便说一句:您的名称空间很奇怪.命名标准建议您在分隔名称空间层次结构时使用点(.
):
BTW: Your namespace is odd. Naming standard suggest that you use the dot (.
) when separating namespace hierarchies:
namespace ConsoleApplication1.ConfigurationEnumerable
这篇关于ConfigurationSection中的自定义IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!