【问题标题】:Reading config sections using Custom config classes in C# and converting them to a list使用 C# 中的自定义配置类读取配置部分并将它们转换为列表
【发布时间】: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.Con‌​fig line 4)

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

谢谢

【问题讨论】:

  • 什么你得到了例外?请发布您收到的完整且准确的错误消息!
  • 无法加载类型 SampleCatalog.FeaturesSection
  • 我错过了什么吗?
  • 你有命名空间SampleCatalog.FeaturesSection吗?
  • SampleCatalog 是命名空间,FeaturesSection 是命名空间内的类。

标签: c# list class config


【解决方案1】:

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

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

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

【讨论】:

  • ConfigurationErrorException: Unrecognized Element 'Feature'
  • @user3581188:用一些代码更改和配置部分结构的更改更新了我的响应;我在 .NET 4.5 上对此进行了测试,它对我有用
  • 错误 18 属性“ConfigurationProperty”在此声明类型上无效。它仅对“属性、索引器”声明有效。
  • @user3581188:你在哪里添加了ConfgurationProperty 属性?我发布的代码有效(我测试它“实时”)
  • [ConfigurationProperty("Features")] public class FeaturesSection : ConfigurationSection { public FeatureCollection Features { get { return (FeatureCollection) base["Features"]; } } }
猜你喜欢
  • 2016-03-27
  • 2015-05-09
  • 2011-06-17
  • 1970-01-01
  • 2018-02-13
  • 2011-07-04
  • 2012-01-28
  • 1970-01-01
  • 2012-10-04
相关资源
最近更新 更多