在ConfigurationSection II中定制IEnumerable [英] Customized IEnumerable in ConfigurationSection II

查看:224
本文介绍了在ConfigurationSection II中定制IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题和代码 C#Customized IEnumerable in ConfigurationSection

使用@Daniel Hilgarth推进解决方案我改变了(app.config):

Advancing in solution to this and with @Daniel Hilgarth answer I have changed (app.config) this:

<section name="Disk"
         type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection"/>

    <section name="Disk"
             type="ConsoleApplication1_ConfigurationEnumerable.PathsConfigSection,
             ConsoleApplication1_ConfigurationEnumerable"/>

现在,我有另一个例外:

Now, I have this another exception:

System.Configuration.ConfigurationErrorsException was unhandled
  HResult=-2146232062
  Message=Unrecognized element 'Path'. (C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config line 9)
  Source=System.Configuration
  BareMessage=Unrecognized element 'Path'.
  Filename=C:\Users\blackberry\Desktop\ConsoleApplication1_ConfigurationEnumerable\ConsoleApplication1_ConfigurationEnumerable\bin\Debug\ConsoleApplication1_ConfigurationEnumerable.vshost.exe.config
  Line=9
  StackTrace:
       at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
       at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
       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:


推荐答案

尝试将以下属性添加到 PathsConfigSection.Paths property:

Try adding the following attribute to your PathsConfigSection.Paths property:

    [ConfigurationCollection(typeof(Paths), AddItemName = "Path")]

现在应该如下所示:

    [ConfigurationProperty("Paths")]
    [ConfigurationCollection(typeof(Paths), AddItemName = "Path")]
    public Paths Paths
    {
        get
        {
            var o = this["Paths"];
            return o as Paths;
        }
    }

这篇关于在ConfigurationSection II中定制IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆