2011-06-16 77 views
13

我无法使自定义配置节正常工作。这是我从网上获得的一些代码,试图更好地理解这个区域,并使我能够达到我最终想要的地方,即我自己的自定义配置部分。包含集合的自定义配置节

我在控制台应用程序中运行代码时遇到的错误是 ' 无法识别的属性“扩展名”。请注意,属性名称区分大小写。

在主应用程序的代码来得到的东西去是

var conf = ConfigurationManager.GetSection("uploadDirector"); 

,这是例外出现在那里。

这是配置部分我希望/努力实现

<AuthorisedClients> 
    <AuthorisedClient name="Client"> 
     <Queue id="1" /> 
     <Queue id="7" /> 
    </AuthorisedClient> 
    <AuthorisedClient name="Client2"> 
     <Queue id="3" /> 
     <Queue id="4" /> 
    </AuthorisedClient> 
    </AuthorisedClients> 

下面是我从网上

config文件

<uploadDirector> 
    <filegroup name="documents" defaultDirectory="/documents/"> 
     <clear/> 
     <add extension="pdf" mime="application/pdf" maxsize="100"/> 
     <add extension="doc" mime="application/word" maxsize="500"/> 
    </filegroup> 
    <filegroup name="images"> 
     <clear/> 
     <add extension="gif" mime="image/gif" maxsize="100"/> 
    </filegroup> 
    </uploadDirector> 

UploadDirectorConfigSection拿到代码.cs

public class UploadDirectorConfigSection : ConfigurationSection { 

     private string _rootPath; 

     public UploadDirectorConfigSection() { 

     } 

     [ConfigurationProperty("rootpath", DefaultValue="/", IsRequired=false, IsKey=false)] 
     [StringValidator([email protected]"[email protected]#$%^&*()[]{};'\|\\")] 
     public string RootPath { 
      get { return _rootPath; } 
      set { _rootPath = value; } 
     } 

     [ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)] 
     public FileGroupCollection FileGroups { 
      get { return (FileGroupCollection) base[""]; } 
      set { base[""] = value; } 
     } 
    } 

FileGroupCollection.cs

public class FileGroupCollection : ConfigurationElementCollection { 
     protected override ConfigurationElement CreateNewElement() { 
      return new FileGroupElement(); 
     } 

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

     public override ConfigurationElementCollectionType CollectionType { 
      get { 
       return ConfigurationElementCollectionType.BasicMap; 
      } 
     } 

     protected override string ElementName { 
      get { 
       return "filegroup"; 
      } 
     } 

     protected override bool IsElementName(string elementName) { 
      if (string.IsNullOrWhiteSpace(elementName) || elementName != "filegroup") 
       return false; 
      return true; 
     } 

     public FileGroupElement this[int index] { 
      get { return (FileGroupElement) BaseGet(index); } 
      set { 
       if(BaseGet(index) != null) 
        BaseRemoveAt(index); 
       BaseAdd(index, value); 
      } 
     } 
    } 

FileGroupElement.cs

public class FileGroupElement : ConfigurationElement { 

     [ConfigurationProperty("name", IsKey=true, IsRequired = true)] 
     [StringValidator(InvalidCharacters = @" [email protected]#$%^&*()[]{}/;'""|\")] 
     public string Name { 
      get { return (string) base["name"]; } 
      set { base["name"] = value; } 
     } 

     [ConfigurationProperty("defaultDirectory", DefaultValue = ".")] 
     public string DefaultDirectory { 
      get { return (string) base["Path"]; } 
      set { base["Path"] = value; } 
     } 

     [ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)] 
     public FileInfoCollection Files { 
      get { return (FileInfoCollection) base[""]; } 
     } 
    } 

FileInfoCollection.cs

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

     protected override object GetElementKey(ConfigurationElement element) { 
      return ((FileInfoElement) element).Extension; 
     } 
    } 

FileInfoElement.cs

public class FileInfoElement : ConfigurationElement { 

     public FileInfoElement() { 
      Extension = "txt"; 
      Mime = "text/plain"; 
      MaxSize = 0; 
     } 

     [ConfigurationProperty("extension", IsKey = true, IsRequired = true)] 
     public string Extension { 
      get { return (string)base["extension"]; } 
      set { base["extension"] = value; } 
     } 

     [ConfigurationProperty("mime", DefaultValue = "text/plain")] 
     public string Mime { 
      get { return (string) base["mime"]; } 
      set { base["mime"] = value; } 
     } 

     [ConfigurationProperty("maxsize", DefaultValue = 0)] 
     public int MaxSize { 
      get { return (int) base["maxsize"]; } 
      set { base["maxsize"] = value; } 
     } 
    } 

回答

30

在方法您的FileInfoCollection定义CreateNewElement创建FileInfoCollection这是不对的。覆盖CreateNewElement应该返回新的集合元素,而不是新的集合:

public class FileInfoCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new FileInfoElement(); 
    } 

    protected override object GetElementKey (ConfigurationElement element) 
    { 
     return ((FileInfoElement)element).Extension; 
    } 
} 

关于你所需的配置,可能是最简单的实现将是这样的:

public class AuthorisedClientsSection : ConfigurationSection { 
    [ConfigurationProperty("", IsDefaultCollection = true)] 
    public AuthorisedClientElementCollection Elements { 
     get { return (AuthorisedClientElementCollection)base[""];} 
    } 
} 

public class AuthorisedClientElementCollection : ConfigurationElementCollection { 
    const string ELEMENT_NAME = "AuthorisedClient"; 

    public override ConfigurationElementCollectionType CollectionType { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 

    protected override string ElementName { 
     get { return ELEMENT_NAME; } 
    } 

    protected override ConfigurationElement CreateNewElement() { 
     return new AuthorisedClientElement(); 
    } 

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

public class AuthorisedClientElement : ConfigurationElement { 
    const string NAME = "name"; 

    [ConfigurationProperty(NAME, IsRequired = true)] 
    public string Name { 
     get { return (string)base[NAME]; } 
    } 

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

public class QueueElementCollection : ConfigurationElementCollection { 
    const string ELEMENT_NAME = "Queue"; 

    public override ConfigurationElementCollectionType CollectionType { 
     get { return ConfigurationElementCollectionType.BasicMap; } 
    } 

    protected override string ElementName { 
     get { return ELEMENT_NAME; } 
    } 

    protected override ConfigurationElement CreateNewElement() { 
     return new QueueElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) { 
     return ((QueueElement)element).Id; 
    } 
} 

public class QueueElement : ConfigurationElement { 
    const string ID = "id"; 

    [ConfigurationProperty(ID, IsRequired = true)] 
    public int Id { 
     get { return (int)base[ID]; } 
    } 
} 

而且测试:

var authorisedClientsSection = ConfigurationManager.GetSection("AuthorisedClients") 
    as AuthorisedClientsSection; 

foreach (AuthorisedClientElement client in authorisedClientsSection.Elements) { 
    Console.WriteLine("Client: {0}", client.Name); 

    foreach (QueueElement queue in client.Elements) { 
     Console.WriteLine("\tQueue: {0}", queue.Id); 
    } 
} 
+0

哇,那也是我的确切问题。谢谢! – 2012-02-01 21:31:02

+0

这也是我的问题。非常感谢你。赶上一个upvoite :-)。 – Oybek 2012-11-14 15:32:12

+0

谢谢先生!完美的,我希望我可以upvote两次! – Jonas 2014-02-11 13:53:22