2011-11-15 97 views
-2

我正在編寫從app.config中讀取自定義配置節的代碼,但我收到一些錯誤,希望任何人都可以幫忙解決。C#配置設置

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <section name="Psptune" 
      type="ConfigurationSettings.PsptuneSection, ConfigurationSettings"/> 
    </configSections> 
    <Psptune> 
    <psp> 
     <FolderLocataions> 
     <add name="PspFolder" directory="aa"/> 
     <add name="IsoFolder" directory="aa"/> 
     </FolderLocataions> 
    </psp> 
    </Psptune> 
    <startup> 
    <supportedRuntime version="v4.0" 
         sku=".NETFramework,Version=v4.0"/> 
    </startup> 
</configuration> 


using System; 

namespace ConfigurationSettings 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       var sec = PsptuneSection.Settings; 
      } 
      catch (Exception exception) 
      { 
       Console.WriteLine(exception); 
      } 
      Console.ReadKey(); 
     } 
    } 
} 
using System.Configuration; 

namespace ConfigurationSettings 
{ 
    public class FolderLocationElement : ConfigurationElement 
    { 
     [ConfigurationProperty("name", DefaultValue = "default", IsRequired = false)] 
     [StringValidator(MinLength = 5, MaxLength = 256)] 
     public string Name 
     { 
      get { return (string) this["name"]; } 
      set { this["name"] = value; } 
     } 

     [ConfigurationProperty("directory", DefaultValue = "default", IsRequired = false)] 
     [StringValidator(MinLength = 5, MaxLength = 256)] 
     public string Directory 
     { 
      get { return (string)this["directory"]; } 
      set { this["directory"] = value; } 
     } 

    } 
} 

using System.Configuration; 

namespace ConfigurationSettings 
{ 
    public class FolderLocationElementCollection : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new FolderLocationElement(); 
     } 

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

     public FolderLocationElement this[int index] 
     { 
      get { return (FolderLocationElement) BaseGet(index); } 
     } 

     public void Add(FolderLocationElement element) 
     { 
      BaseAdd(element); 
     } 
    } 
} 

using System.Configuration; 

namespace ConfigurationSettings 
{ 
    public class PspElement : ConfigurationElement 
    { 
     [ConfigurationProperty("FolderLocataions", DefaultValue = "default", IsRequired = false)] 
     [StringValidator(MinLength = 5, MaxLength = 256)] 
     public FolderLocationElementCollection FolderLocataions 
     { 
      get { return (FolderLocationElementCollection) this["FolderLocataions"]; } 
      set { this["FolderLocataions"] = value; } 
     } 
    } 
} 

using System.Configuration; 

namespace ConfigurationSettings 
{ 
    public class PsptuneSection : ConfigurationSection 
    { 
     private static PsptuneSection _settings = 
      ConfigurationManager.GetSection("Psptune") as PsptuneSection; 

     public static PsptuneSection Settings { get { return _settings; } } 

     [ConfigurationProperty("psp", DefaultValue = "default", IsRequired = false)] 
     public PspElement Psp 
     { 
      get { return (PspElement) this["psp"]; } 
      set { this["psp"] = value; } 
     } 
    } 
} 

我得到的exeception是:

System.TypeInitializationException: The type initializer for 'ConfigurationSetti ngs.PsptuneSection' threw an exception. ---> System.Configuration.ConfigurationE rrorsException: The default value of the property 'psp' cannot be parsed. The er ror is: Object reference not set to an instance of an object.

+0

沒關係我的朋友我知道了,你可以停下來投票給我。 問題是有些元素不能有默認值。 – retide

+0

+1:看不出有什麼理由投下來。希望你至少回到0。 –

回答

0

沒關係我的朋友,我得到它弄清楚,你脂肪酶可以停下來投我失望。問題是一些元素不能有默認值

+0

「某些元素」非常含糊。你能更具體一些,這是一個沒用的可怕例子。 –