2013-02-07 21 views
0

原來的問題和代碼Customized IEnumerable in ConfigurationSection中的ConfigurationSection定製的IEnumerable II

推進在解決這一與@Daniel Hilgarth答案我已經改變(app.config)中這樣的:

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

本之一:

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

現在,我這還有一個例外:

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: 
+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

嘗試添加下列屬性的PathsConfigSection.Paths屬性:

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

現在應該是這樣的:

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

+1您的建議絕對是現貨。 –

+0

@Monkieboy:謝謝。但我不確定你爲什麼重複我的答案。你看到了什麼價值? –

+0

我無法融入評論,@Ferpega並未將您的答案標記爲完整,我假設他沒有找到解決方案。我將您的建議作爲提供解決方案的平臺,如果您覺得我沒有爲此問題增加清晰度或價值,我很樂意刪除我的答案。 –

1

你的配置應該是現在這個樣子:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="Disk" type="ConsoleApplication1.DiskConfigSection, ConsoleApplication1"/> 
    </configSections> 
    <Disk> 
    <Paths> 
     <Path name="one" permission="1" /> 
     <Path name="two" permission="2" /> 
     <Path name="three" permission="3" /> 
    </Paths> 
    </Disk> 
</configuration> 

你的課堂現在應該廁所k像這樣:

using System.Configuration; 

namespace ConsoleApplication1 
{ 
class Program 
{ 
    static void Main(string[] args) 
    { 
     var t = DiskConfigSection.GetConfig(); 
     // Put a breakpoint on the line after this and put a watch on t 
    } 
} 

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 BaseGet(index) as Path; 
     } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 

    protected override ConfigurationElement CreateNewElement() 
    { 
     return new Path(); 
    } 
    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((Path)element).Name; 
    } 
} 

public class DiskConfigSection : ConfigurationSection 
{ 
    public static DiskConfigSection GetConfig() 
    { 
     var b = ConfigurationManager.GetSection("Disk") != null; 
     return b ? (DiskConfigSection)ConfigurationManager.GetSection("Disk") : new DiskConfigSection(); 
    } 

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

我已經在控制檯應用程序中進行了測試,配置完全加載。

+0

信用@DanielHilgarth這是因爲他的意見,我能夠構建這一點。 –

相關問題