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

Reading config sections using Custom config classes in C# and converting them to a list

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

我在 class 中的代码如下:

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

我遇到了异常。

An error occurred creating the configuration section handler for FeaturesSection: Could not load type 'SampleCatalog.FeaturesSection' from assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. (C:\Users\Venkata_Poruri_Pavan\Documents\Visual Studio 2013\Projects\SampleCatalog\SampleCatalog\bin\Debug\SampleCatalog.vshost.exe.Con‌​fig line 4)

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

谢谢

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

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

您需要指定 type= 为 fully-qualified class 名称,然后在逗号后定义 assembly 类型存储在。如果您省略它(就像您在 post 中所做的那样),.NET 将签入 System.Configuration 程序集 - 但当然,它不会找到您的自定义 class那里!

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

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

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

在您的 FeatureCollection class 上,您需要定义(使用属性)集合将包含的元素的**类型*,以及集合中各个元素的名称:

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

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