【发布时间】:2016-01-06 21:39:49
【问题描述】:
我的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>
我在课堂上的代码如下:
public class FeaturesSection:ConfigurationSection
{
public FeatureCollection Features
{
get{return (FeatureCollection)base["Features"};
}
}
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);
}
}
public class Feature: ConfigurationElement
{
[ConfigurationProperty("Name",IsRequired=true)]
public string Name {get; set;}
[ConfigurationProperty("US",IsRequired=true)]
public bool US {get; set;}
[ConfigurationProperty("EM",IsRequired=true)]
public bool EM {get; set;}
[ConfigurationProperty("Sequence",IsRequired=true)]
public string Sequence {get; set;}
}
现在当我运行这段代码时:
var mysection = (FeaturesSection)ConfigurationManager.GetSection("FeaturesSection");
我遇到了异常。
为 FeaturesSection 创建配置节处理程序时出错:无法从程序集“System.Configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“SampleCatalog.FeaturesSection”。 (C:\Users\Venkata_Poruri_Pavan\Documents\Visual Studio 2013\Projects\SampleCatalog\SampleCatalog\bin\Debug\SampleCatalog.vshost.exe.Config line 4)
请帮忙,请接受我的歉意,因为我无法在此处粘贴代码。
谢谢
【问题讨论】:
-
什么你得到了例外?请发布您收到的完整且准确的错误消息!
-
无法加载类型 SampleCatalog.FeaturesSection
-
我错过了什么吗?
-
你有命名空间
SampleCatalog.FeaturesSection吗? -
SampleCatalog 是命名空间,FeaturesSection 是命名空间内的类。