无法识别的元素"项目"在配置文件中的自定义配置节 [英] Unrecognized element "Item" in config file with custom config section
问题描述
我这是基于某些类的自定义配置。我的问题是,我得到一个错误说,一个配置元素是无法识别的。类是如下:
[ConfigurationCollection(typeof运算(SectionItem),AddItemName =项,CollectionType = ConfigurationElementCollectionType.BasicMap) ]
公共类展区:ConfigurationElementCollection中
{
公共SectionItem这个[INT指数]
{
{返回BaseGet(指数)为SectionItem; }
组
{
如果(BaseGet(指数)!= NULL)
{
BaseRemoveAt(指数);
}
BaseAdd(指数值);
}
}
公开新SectionItem此[字符串回应]
{
{返回(SectionItem)BaseGet(响应); }
设置
{
如果(BaseGet(响应)!= NULL)
{
BaseRemoveAt(BaseIndexOf(BaseGet(响应)));
}
BaseAdd(值);
}
}
保护覆盖的ConfigurationElement CreateNewElement()
{
返回新SectionItem();
}
保护覆盖对象GetElementKey(的ConfigurationElement元素)
{
回报率((SectionItem)元素)。重点;
}
}
以及 SectionItem
类:
公共类SectionItem:的ConfigurationElement
{
[的ConfigurationProperty(键IsRequired = TRUE,IsKey = TRUE)]
公共字符串键
{
{返回此[钥匙]作为字符串; }
}
}
如果我添加一个类型<的配置属性code> SectionItems 在节
类,因为我想有多个,不会为我工作, SectonItems
中的部分
在配置文件中的标记。我搜索了解决方案,但一切我发现没有做这个的伎俩。
。对于更好地理解我想要实现这个是我的配置的外观:
<结构> ;
< configSections>
<节名称=的AdminConfigTYPE =XmlTest.AdminConfig,Xmltest的/>
< / configSections>
<&启动GT;
< supportedRuntime版本=V4.0SKU =.net框架,版本= V4.5/>
< /启动>
<&的AdminConfig GT;
<字段名称=字段1键=12345路径=ASD/>
<节>
<项目关键=12345/>
<项目关键=54321/>
< /节>
< /&的AdminConfig GT;
< /结构>
行,所以我已经找到了问题。虽然节
类有这个 [ConfigurationCollection(typeof运算(SectionItem),AddItemName =项,CollectionType = ConfigurationElementCollectionType.BasicMap)]
我必须标注在配置节
类的属性,如下所示:
[的ConfigurationProperty(科)]
[ConfigurationCollection(typeof运算(段),AddItemName =项目)]
公众节段
{
GET
{
回报(段)这个[条];
}
}
现在该项目的认可和东西正常工作。
I have a custom config which is based on some classes. My problem is that I get an error saying that a config element is unrecognized. The class is as follows:
[ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class Sections : ConfigurationElementCollection
{
public SectionItem this[int index]
{
get { return BaseGet(index) as SectionItem; }
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
public new SectionItem this[string response]
{
get { return (SectionItem)BaseGet(response); }
set
{
if (BaseGet(response) != null)
{
BaseRemoveAt(BaseIndexOf(BaseGet(response)));
}
BaseAdd(value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new SectionItem();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((SectionItem)element).Key;
}
}
And the SectionItem
class:
public class SectionItem : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get { return this["key"] as string; }
}
}
If I add a configuration property of type SectionItems
in the Sections
class that won't work for me, because I want to have multiple SectonItems
inside the Section
tag in the config file. I've searched for solutions but everything I've found didn't do the trick with this.
For a better understanding of what I'm trying to achieve this is how my config looks:
<configuration>
<configSections>
<section name="AdminConfig" type="XmlTest.AdminConfig, XmlTest"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<AdminConfig>
<Field name="field1" key="12345" path="asd"/>
<Section>
<Item key="12345"/>
<Item key="54321"/>
</Section>
</AdminConfig>
</configuration>
OK so I've found the problem. Although the Sections
class had this [ConfigurationCollection(typeof(SectionItem), AddItemName = "Item", CollectionType = ConfigurationElementCollectionType.BasicMap)]
I had to annotate the property in the ConfigurationSection
class, as follows:
[ConfigurationProperty("Section")]
[ConfigurationCollection(typeof(Sections), AddItemName = "Item")]
public Sections Sections
{
get
{
return (Sections)this["Section"];
}
}
Now the items are recognized and stuff is working properly.
这篇关于无法识别的元素"项目"在配置文件中的自定义配置节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!