含收集自定义配置节 [英] Custom config section containing collection
问题描述
我无法得到一个自定义的配置部分工作。这是一些code我从网上在努力试着去了解这方面的更好一点,使我能够得到的地方,我想ultimatly是得到了,我自己的自定义配置部分。
当我在一个控制台应用程序运行code我得到的错误是 无法识别的属性扩展。需要注意的是属性名称是区分大小写的。的
在code,在主要的应用程序可以让事情就是
VAR的conf = ConfigurationManager.GetSection(uploadDirector);
这是出现异常的地方。
这是配置部分我希望/试图实现
< AuthorisedClients>
< AuthorisedClient名=客户端>
<队列ID =1/>
<队列ID =7/>
< / AuthorisedClient>
< AuthorisedClient名=客户机2>
<队列ID =3/>
<队列ID =4/>
< / AuthorisedClient>
< / AuthorisedClients>
这里的code我已经从网上得到了
config文件的
< uploadDirector>
<文件组名称=文档defaultDirectory =/文件/>
<清/>
<添加扩展=PDF哑剧=应用程序/ PDFMAXSIZE =100/>
<添加扩展=DOC哑剧=应用/词MAXSIZE =500/>
< /文件组>
<文件组名称=图片>
<清/>
<添加扩展=GIF哑剧=图像/ GIFMAXSIZE =100/>
< /文件组>
< / uploadDirector>
UploadDirectorConfigSection.cs 的
公共类UploadDirectorConfigSection:配置节{
私人字符串_rootPath;
公共UploadDirectorConfigSection(){
}
[的ConfigurationProperty(ROOTPATH,默认值=/,IsRequired =假,IsKey = FALSE)]
[StringValidator(InvalidCharacters = @〜@#$%^&放大器; *()[] {};!'\ | \\)
公共字符串ROOTPATH {
{返回_rootPath; }
集合{_rootPath =价值; }
}
[的ConfigurationProperty(,IsRequired = TRUE,IsKey =假,IsDefaultCollection =真)
公共FileGroupCollection文件组{
{返回(FileGroupCollection)基地(); }
集合{基地[] =价值; }
}
}
FileGroupCollection.cs 的
公共类FileGroupCollection:ConfigurationElementCollection {
保护覆盖的ConfigurationElement CreateNewElement(){
返回新FileGroupElement();
}
保护覆盖对象GetElementKey(的ConfigurationElement元){
返回((FileGroupElement)元).Name点;
}
公众覆盖ConfigurationElementCollectionType CollectionType {
得到 {
返回ConfigurationElementCollectionType.BasicMap;
}
}
保护覆盖字符串的ElementName {
得到 {
返回文件组;
}
}
布尔IsElementName(字符串的ElementName)保护覆盖{
如果(string.IsNullOrWhiteSpace(的ElementName)||的ElementName!=文件组)
返回false;
返回true;
}
公共FileGroupElement这个[INT指数] {
{返回(FileGroupElement)BaseGet(指数); }
组 {
如果(BaseGet(指数)!= NULL)
BaseRemoveAt(指数);
BaseAdd(指数值);
}
}
}
FileGroupElement.cs 的
公共类FileGroupElement:{的ConfigurationElement
[的ConfigurationProperty(名,IsKey = TRUE,IsRequired =真)
[StringValidator(InvalidCharacters = @。〜!@#$%^&放大器; *()[] {} /;| \)
公共字符串名称{
{返回(串)基地[名称]; }
集合{基地[名称] =价值; }
}
[的ConfigurationProperty(defaultDirectory,默认值=。)]
公共字符串DefaultDirectory {
{返回(串)基地[路径]; }
集合{基地[路径] =值; }
}
[的ConfigurationProperty(,IsDefaultCollection = TRUE,IsRequired =真)
公共FileInfoCollection文件{
{返回(FileInfoCollection)基地(); }
}
}
FileInfoCollection.cs 的
公共类FileInfoCollection:ConfigurationElementCollection {
保护覆盖的ConfigurationElement CreateNewElement(){
返回新FileInfoCollection();
}
保护覆盖对象GetElementKey(的ConfigurationElement元){
返程((FileInfoElement)元素)。扩展名;
}
}
FileInfoElement.cs 的
公共类FileInfoElement:{的ConfigurationElement
公共FileInfoElement(){
扩展=TXT;
哑剧=text / plain的;
MAXSIZE = 0;
}
[的ConfigurationProperty(扩展名,IsKey = TRUE,IsRequired =真)
公共字符串扩展{
{返回(串)基地[扩展名]; }
集合{基地[扩展名] =值; }
}
[的ConfigurationProperty(哑剧,默认值=text / plain的)]
公共字符串哑剧{
{返回(串)基地[哑剧]; }
集合{基地[哑剧] =值; }
}
[的ConfigurationProperty(最大范围,默认值= 0)]
公众诠释MAXSIZE {
{返回(INT)基地[MAXSIZE]; }
集合{基地[MAXSIZE] =值; }
}
}
在该方法中的 FileInfoCollection
定义 CreateNewElement
创建 FileInfoCollection
这是不对的。覆盖 CreateNewElement
应该返回新的集合元素,而不是新的集合:
公共类FileInfoCollection:ConfigurationElementCollection
{
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新FileInfoElement();
}
保护覆盖对象GetElementKey(的ConfigurationElement元)
{
返程((FileInfoElement)元素)。扩展名;
}
}
关于你想要的配置,可能是最简单的实现将是这样的:
公共类AuthorisedClientsSection:配置节{
[的ConfigurationProperty(,IsDefaultCollection =真)
公共AuthorisedClientElementCollection元素{
{返回(AuthorisedClientElementCollection)基地();}
}
}
公共类AuthorisedClientElementCollection:ConfigurationElementCollection {
常量字符串ELEMENT_NAME =AuthorisedClient;
公众覆盖ConfigurationElementCollectionType CollectionType {
{返回ConfigurationElementCollectionType.BasicMap; }
}
保护覆盖字符串的ElementName {
{返回ELEMENT_NAME; }
}
保护覆盖的ConfigurationElement CreateNewElement(){
返回新AuthorisedClientElement();
}
保护覆盖对象GetElementKey(的ConfigurationElement元){
返回((AuthorisedClientElement)元).Name点;
}
}
公共类AuthorisedClientElement:{的ConfigurationElement
常量字符串NAME =名;
[的ConfigurationProperty(NAME,IsRequired =真)
公共字符串名称{
{返回(串)的基础[NAME] }
}
[的ConfigurationProperty(,IsDefaultCollection =真)
公共QueueElementCollection元素{
{返回(QueueElementCollection)基地(); }
}
}
公共类QueueElementCollection:ConfigurationElementCollection {
常量字符串ELEMENT_NAME =队列;
公众覆盖ConfigurationElementCollectionType CollectionType {
{返回ConfigurationElementCollectionType.BasicMap; }
}
保护覆盖字符串的ElementName {
{返回ELEMENT_NAME; }
}
保护覆盖的ConfigurationElement CreateNewElement(){
返回新QueueElement();
}
保护覆盖对象GetElementKey(的ConfigurationElement元){
返回((QueueElement)元).ID;
}
}
公共类QueueElement:{的ConfigurationElement
常量字符串ID =ID;
[的ConfigurationProperty(ID,IsRequired =真)
公众诠释ID为{
{返回(INT)基地[ID] }
}
}
和测试:
VAR authorisedClientsSection = ConfigurationManager.GetSection(AuthorisedClients)
作为AuthorisedClientsSection;
的foreach(在authorisedClientsSection.Elements AuthorisedClientElement客户端){
Console.WriteLine(客户:{0},client.Name);
的foreach(在client.Elements QueueElement队列){
Console.WriteLine(\ TQUEUE:{0},queue.Id);
}
}
I'm having trouble getting a custom config section to work. It's some code I got from the web in an effort to try to understand this area a little better and enable me to get to where I want to ultimatly be, my own custom config section.
The error I get when I run the code in a console app is ' Unrecognized attribute 'extension'. Note that attribute names are case-sensitive.'
The code in the main app to get things going is
var conf = ConfigurationManager.GetSection("uploadDirector");
and this is where the exception appears.
This is the config section I am hoping/trying to achieve
<AuthorisedClients>
<AuthorisedClient name="Client">
<Queue id="1" />
<Queue id="7" />
</AuthorisedClient>
<AuthorisedClient name="Client2">
<Queue id="3" />
<Queue id="4" />
</AuthorisedClient>
</AuthorisedClients>
Here's the code I have got from the web
.config file
<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(InvalidCharacters=@"~!.@#$%^&*()[]{};'\|\\")]
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 = @" ~.!@#$%^&*()[]{}/;'""|\")]
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; }
}
}
In your definition of FileInfoCollection
in the method CreateNewElement
you create FileInfoCollection
which is wrong. Overridden CreateNewElement
should return new collection element, not the new collection:
public class FileInfoCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new FileInfoElement();
}
protected override object GetElementKey (ConfigurationElement element)
{
return ((FileInfoElement)element).Extension;
}
}
Regarding your desired configuration, probably the simplest implementation will look like:
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]; }
}
}
And the test:
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);
}
}
这篇关于含收集自定义配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!