使用C#中的自定义配置类读取配置节并将其转换为列表
我的使用C#中的自定义配置类读取配置节并将其转换为列表,c#,list,class,config,C#,List,Class,Config,我的app.config文件中有: <Configuration> <configsections> <section name="FeaturesSection" type="SampleCatalog.FeaturesSection" /> </configsections> <FeaturesSection> <Feature Name="CCH" US="true" EM="false" Sequence=
app.config
文件中有:
<Configuration>
<configsections>
<section name="FeaturesSection" type="SampleCatalog.FeaturesSection" />
</configsections>
<FeaturesSection>
<Feature Name="CCH" US="true" EM="false" Sequence="1" />
<Feature Name="PLT" US="true" EM="false" Sequence="1" />
<Feature Name="PD" US="true" EM="false" Sequence="1" />
</FeaturesSection>
<Configuration>
现在,当我运行此代码时:
var mysection = (FeaturesSection)ConfigurationManager.GetSection("FeaturesSection");
我有例外
创建FeaturesSection的配置节处理程序时出错:无法从程序集“System.configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“SampleCatalog.FeaturesSection”。(C:\Users\Venkata\u Poruri\u Pavan\Documents\Visual Studio 2013\Projects\SampleCatalog\SampleCatalog\bin\Debug\SampleCatalog.vshost.exe.Con图4)
请帮忙,请接受我的道歉,因为我无法在这里粘贴代码
谢谢如果您自己的程序集中有配置节类型,则需要在
中定义该程序集-请尝试使用以下方法:
<configsections>
<section name="FeaturesSection"
type="SampleCatalog.FeaturesSection, SampleCatalog" />
</configsections>
在FeatureCollection
类中,您需要定义(使用属性)集合将包含的**类型*元素,以及集合中各个元素的调用方式:
[ConfigurationCollection(typeof(Feature), AddItemName = "Feature")]
public class FeatureCollection : ConfigurationElementCollection
{
public Feature this[int index]
{
get { return (Feature)BaseGet(index); }
set
{
if(BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index,value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new Feature();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((Feature)element);
}
}
然后您的配置需要如下所示:
[ConfigurationProperty("Features")]
public class FeaturesSection : ConfigurationSection
{
public FeatureCollection Features
{
get{return (FeatureCollection)base["Features"};
}
}
<Configuration>
<configSections>
<!-- add the ASSEMBLY after the fully-qualified class name -->
<section name="FeaturesSection"
type="SampleCatalog.FeaturesSection, SampleCatalog" />
</configSections>
<FeaturesSection>
<!-- this is the name defined on the FeaturesSection -->
<Features>
<Feature Name="CCH" US="true" EM="false" Sequence="1" />
<Feature Name="PLT" US="true" EM="false" Sequence="1" />
<Feature Name="PD" US="true" EM="false" Sequence="1" />
</Features>
</FeaturesSection>
<Configuration>
通过此设置,您应该能够正确读取自定义配置部分。您会遇到哪些异常?请发布您收到的完整且准确的错误消息!无法加载类型SampleCatalog.FeaturesSection我是否遗漏了任何内容?是否有命名空间
SampleCatalog.FeaturesSection
?SampleCatalog是命名空间,FeaturesSection是命名空间中的类。ConfigurationErrorException:无法识别的元素“Feature”@user3581188:使用一些代码更改更新了我的响应以及配置部分的结构变化;我在.NET 4.5上对此进行了测试,它适用于meError 18。属性“ConfigurationProperty”对此声明类型无效。它仅对“property,indexer”声明有效。@user3581188:您在哪里添加了configurationProperty
属性?我发布的代码可以正常工作(我测试为“live”)[ConfigurationProperty(“Features”)]公共类功能部分:配置部分{public FeatureCollection Features{get{return(FeatureCollection)base[“Features”];}}}
<Configuration>
<configSections>
<!-- add the ASSEMBLY after the fully-qualified class name -->
<section name="FeaturesSection"
type="SampleCatalog.FeaturesSection, SampleCatalog" />
</configSections>
<FeaturesSection>
<!-- this is the name defined on the FeaturesSection -->
<Features>
<Feature Name="CCH" US="true" EM="false" Sequence="1" />
<Feature Name="PLT" US="true" EM="false" Sequence="1" />
<Feature Name="PD" US="true" EM="false" Sequence="1" />
</Features>
</FeaturesSection>
<Configuration>