从App.config中的自定义配置中读取

时间:2016-11-29 13:22:44

标签: c# visual-studio-2010 app-config custom-configuration

我在从App.config读取时遇到问题。

这是我的App.config:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Interest
{
  public class InterestRate_A : ConfigurationElement
  {
    [ConfigurationProperty("band", IsRequired = true)]
    public string Band
    {
      get
      {
        return this["band"] as string;
      }
    }

    [ConfigurationProperty("validFrom", IsRequired = true)]
    public string ValidFrom
    {
      get
      {
        return this["ValidFrom"] as string;
      }
    }

    [ConfigurationProperty("validTo", IsRequired = true)]
    public string ValidTo
    {
      get
      {
        return this["validTo"] as string;
      }
    }

    [ConfigurationProperty("rate", IsRequired = true)]
    public string Rate
    {
      get
      {
        return this["rate"] as string;
      }
    }
  }

  public class InterestRates_A : ConfigurationElementCollection
  {
    public InterestRate_A this[int index]
    {
      get
      {
        return base.BaseGet(index) as InterestRate_A;
      }
      set
      {
        if (base.BaseGet(index) != null)
        {
          base.BaseRemoveAt(index);
        }
        this.BaseAdd(index, value);
      }
    }

    public new InterestRate_A this[string responseString]
    {
      get { return (InterestRate_A)BaseGet(responseString); }
      set
      {
        if (BaseGet(responseString) != null)
        {
          BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
        }
        BaseAdd(value);
      }
    }

    protected override System.Configuration.ConfigurationElement CreateNewElement()
    {
      return new InterestRate_A();
    }

    protected override object GetElementKey(System.Configuration.ConfigurationElement element)
    {
      return ((InterestRate_A)element).Band;
    }
  }

  public class InterestRates_A_Configuration : ConfigurationSection
  {
    public static InterestRates_A_Configuration GetConfig()
    {
      return (InterestRates_A_Configuration)System.Configuration.ConfigurationManager.GetSection("InterestRates_A") ?? new InterestRates_A_Configuration();
    }

    [System.Configuration.ConfigurationProperty("InterestRates_A")]
    [ConfigurationCollection(typeof(InterestRates_A), AddItemName = "InterestRate_A")]
    public InterestRates_A InterestRates_A
    {
      get
      {
        object o = this["InterestRates_A"];
        return o as InterestRates_A;
      }
    }
  }
}

这是我的代码:

  foreach (var item in config.InterestRates_A)
  {

  }

这就是我所说的:

object Test {
  @inline def unapply(i: Int): Option[String] =
    i match {
      case 1 => Some("Got 1")
      case 2 => Some("Got 2")
      case 3 => throw new Exception("Should not test 3")
      case _ => None
  }
  def test(i: Int) = i match {
    case Test(k) => k
    case 4 => "Another 4"
    case _ => ""
  }
}
Test.test(3)

问题在于它根本找不到该部分。我在这里错过了什么?提前感谢所有需要时间帮助我的人!

PS:一旦我将听到的声音包裹起来,我还想添加更多配置部分。

1 个答案:

答案 0 :(得分:0)

如果没有对代码进行详细检查,看起来您的部分类型没有正确定义,请像type="Interest.InterestRates_A_Configuration"

顺便说一句,您最好阅读这篇深入的文章,其中涵盖了您现在需要的.net配置。

Unraveling the Mysteries of .NET 2.0 Configuration

相关问题