使用C#中的自定义配置类读取配置节并将它们转换为列表

时间:2016-01-06 21:39:50

标签: c# list class config

我在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.Con fig line 4)

请帮助,请接受我的道歉,因为我无法在此处粘贴代码。

由于

1 个答案:

答案 0 :(得分:1)

如果您在自己的程序集中有配置部分类型,则需要在<configSection>中定义该程序集 - 尝试使用此:

<configsections>
   <section name="FeaturesSection" 
            type="SampleCatalog.FeaturesSection, SampleCatalog" />
</configsections>

您需要将type=指定为完全限定的类名,然后在逗号后面,定义存储该类型的程序集。如果省略它(就像你一样)在你的帖子中),.NET会检查System.Configuration程序集 - 当然,它不会在那里找到你的自定义类!

更新:确定您的代码和配置需要一些小调整:

FeaturesSection上,您需要添加ConfigurationProperty属性来定义将以什么名称存储条目集合 - 如下所示:

[ConfigurationProperty("Features")]
public class FeaturesSection : ConfigurationSection
{
    public FeatureCollection Features
    {
        get{return (FeatureCollection)base["Features"};
    }
}

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);
    }
}

然后你的配置需要看起来像这样:

<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>

使用此设置,您应该能够正确读出自定义配置部分。