2011-11-02 50 views
1

我正在嘗試創建一個如下所示的自定義配置。無法讓自定義配置部分工作

<configuration> 
    <configSections> 
    <section name="actions" type="ConfigurationTest.ActionsConfig, UnitTestExperiments" /> 
    <section name="action" type="ConfigurationTest.ActionConfig, UnitTestExperiments"/> 
    </configSections> 
    <actions poolSize="100"> 
    <action name="a1" impl="some.class1"> 
     <add name="key11" value="value11"/> 
     <add name="key12" value="key12"/> 
    </action> 
    <action name="a2" impl="some.class2"> 
     <add name="key21" value="value21"/> 
     <add name="key22" value="key22"/> 
    </action> 
    </actions> 
</configuration> 

這裏<action>標記可以出現N次,每個動作都會有一組唯一的鍵。我嘗試了幾種不同的解決方案和選項,但似乎我無法獲得正確的類結構/映射。我希望能得到一個我可以這樣稱呼的結構。

using System; 
    using System.Configuration; 

    namespace ConfigurationTest 
    { 
     public class ActionsConfig : ConfigurationSection 
     { 
      [ConfigurationProperty("poolSize")] 
      public string PoolSize 
      { 
       get { return (string)this["poolSize"]; } 
      } 

      [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)] 
      public ActionList Instances 
      { 
       get { return (ActionList)this[""]; } 
       set { this[""] = value; } 
      } 
     } 

     public class ActionList : ConfigurationElementCollection 
     { 
      protected override ConfigurationElement CreateNewElement() 
      { 
       return new ActionConfig(); 
      } 

      protected override object GetElementKey(ConfigurationElement element) 
      { 
       return ((ActionConfig)element).Name; 
      } 
     } 

     public class ActionConfig : ConfigurationSection 
     { 
      [ConfigurationProperty("name")] 
      public string Name 
      { 
       get { return (string)this["name"]; } 
      } 

      [ConfigurationProperty("impl")] 
      public string Impl 
      { 
       get { return (string)this["impl"]; } 
      } 

      [ConfigurationProperty("", IsDefaultCollection = true)] 
      public NameValueConfigurationCollection Settings 
      { 
       get 
       { 
        return (NameValueConfigurationCollection)base[""]; 
       } 
      } 
     } 



    public class Program 
     { 
      static void Main(string[] args) 
      { 
       ActionsConfig ac = ConfigurationManager.GetSection("actions") as ActionsConfig; 
       Console.WriteLine(ac.PoolSize); 
       ActionConfig nameValueSection = ac.CurrentConfiguration.GetSection("action") as ActionConfig; 
       NameValueConfigurationCollection settings = nameValueSection.Settings; 
       foreach (var key in settings.AllKeys) 
       { 
        Console.WriteLine(settings[key].Name + ": " + settings[key].Value); 
       } 
      } 
     } 
    } 

異常跟蹤如下。

System.Configuration.ConfigurationErrorsException was unhandled 
    Message=Unrecognized element 'action'. (UnitTestExperiments.vshost.exe.Config line 8) 
    Source=System.Configuration 
    BareMessage=Unrecognized element 'action'. 
    Filename=UnitTestExperiments.vshost.exe.Config 
    Line=8 
    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.ConfigurationManager.GetSection(String sectionName) 
     at ConfigurationTest.Program.Main(String[] args) in C:\Users\me\Documents\Visual Studio 2010\Projects\UnitTestExperiments\Program.cs:line 63 
     at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 
+0

究竟你有什麼問題?是否有例外?你能顯示配置部分的定義嗎? –

+0

更新了內容 –

+0

您是否找到解決此問題的方法? – Dorin

回答

-4

我從示範項目確切的答案Here

我不得不代碼擴展到一個額外層讓它爲我工作。

3

看看使用Custom Configuration Settings。這有一個很好的例子。

更新

添加自定義的部分看看例子Custom ConfigurationSections in .NET 2.0 .config Files

+0

我期待獲得一個類結構,它允許我像鍵值對那樣訪問''屬性。 –

+0

我想使用''屬性,如鍵值對,它的工作原理。我無法將Actions標籤綁定到N個Action標籤,然後像keyvalue對那樣訪問屬性 –

相關問題