简体   繁体   English

C#AppSettings:有没有简单的方法可以将集合放入<appSetting>

[英]C# AppSettings: Is there a easy way to put a collection into <appSetting>

i tried 我试过了

<appSettings >
    <add key="List" value="1"/>
    <add key="List" value="2"/>
    <add key="List" value="3"/>
  </appSettings >

and System.Configuration.ConfigurationManager.AppSettings.GetValues("List"); System.Configuration.ConfigurationManager.AppSettings.GetValues("List");

But i only get the last member . 但是我只有最后一个成员。 How could i solve this easily? 我该如何轻松解决此问题?

I have dealt a similar issue and I did it with this code. 我已经处理了类似的问题,并使用此代码完成了此工作。 Hope this helps in your problem. 希望这对您的问题有所帮助。

In this case List (similar to my URLSection) will have a full configuration Section in web.config which you can get all values from this section then. 在这种情况下,列表(类似于我的URLSection)将在web.config中具有完整的配置节,您可以从该节中获取所有值。

<configSections>
    <section name="URLSection" type="A.WebConfigSection,A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>

<appSettings></appSettings>

<URLSection>
    <urlCollection>
        <add url="1" value="a"/>
        <add url="2" value="b"/>
    </urlCollection>
</URLSection>

I made three classes for this: ConfigElement, ConfigElementCollection, WebConfigSection. 为此,我制作了三个类:ConfigElement,ConfigElementCollection,WebConfigSection。

ConfigElement ConfigElement

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

namespace A
{
  public class ConfigElement:System.Configuration.ConfigurationElement
{
    [ConfigurationProperty("url",IsRequired=true) ]
    public string url
    {
        get
        {
            return this["url"] as string;
        }
    }

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



  }
}

ConfigElementCollection ConfigElementCollection

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

namespace A
{
  public class ConfigElementCollection:ConfigurationElementCollection
 {
    public ConfigElement this[int index]
    {
        get
        {
            return base.BaseGet(index) as ConfigElement;
        }

    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new ConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((ConfigElement)(element)).url;
    }
 }
}

WebConfigSection WebConfig部分

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

namespace A
{
 public class WebConfigSection:ConfigurationSection
 {

    public WebConfigSection()
    {

    }

    [ConfigurationProperty("urlCollection")]
    public ConfigElementCollection allValues
    {
        get
        {
            return this["urlCollection"] as ConfigElementCollection;
        }
    }

    public static WebConfigSection GetConfigSection()
    {
        return ConfigurationSettings.GetConfig("URLSection") as WebConfigSection;
    }
 }
}
    foreach (string str in ConfigurationManager.AppSettings.AllKeys)
    {
        if (str.ToUpper().IndexOf("SOMESPECIAL") > -1) //the somespecial ones you want to add in
            lstList.Add(ConfigurationManager.AppSettings[str]);
    }

NinjaSettings does this out of the box. NinjaSettings开箱即用。

In the package manager console 在包管理器控制台中

Install-Package NinjaSettings

You would declare your list as 您可以将清单声明为

  <appSettings>
    <add key="List" value="50,20,10,100"/>
  </appSettings>

then create an Interface with a mapping for list to any ICollection or Array 然后使用映射列表创建一个接口,以映射到任何ICollection或数组

public interface IAppSettings
{
    List<int> List { get; }
}

then access your settings user the NinjaSettings wrapper. 然后访问您的设置用户NinjaSettings包装器。 Generally you would wire this up using IOC, but the basic usage is 通常,您可以使用IOC进行连接,但是基本用法是

   var settings = new NinjaSettings<IAppSettings>().Settings;

   int total = 0;
   for (var i in settings.List) 
   {
      total+=i;        
   }

You'd likely be better off putting this information in a separate XML file and having a reference to that file in AppSettings. 您最好将这些信息放在一个单独的XML文件中,并在AppSettings中对该文件进行引用。 That would give you a lot more flexibility around how you retrieved the information and consumed it. 这样一来,您可以在信息检索和使用方式方面更加灵活。

The only thing would be that you'd want to create a separate (static?) class for reading the XML in a similar fashion to the System.Configuration.ConfigurationManager.AppSettings class. 唯一的问题是,您想要创建一个单独的(静态?)类以类似于System.Configuration.ConfigurationManager.AppSettings类的方式读取XML。

If, on the other hand, it HAD to be in your Web.Config file, I would suggest the only way to achieve this simply would be to have a [pipe/comma/semi-colon] delimited array in one "List" setting. 另一方面,如果它必须位于您的Web.Config文件中,则建议实现此目的的唯一方法就是在一个“列表”设置中使用[pipe /逗号/分号]分隔的数组。

Haacked provides a concise approach to configuration settings. Haacked提供了一种简洁的配置设置方法。 His approach uses one class deriving from ConfigurationSection. 他的方法使用从ConfigurationSection派生的一类。 So for his blog example your app.config or web.config xml representation will look like this: 因此,对于他的博客示例,您的app.config或web.config xml表示形式将如下所示:

<configuration>
  <configSections>
    <section name="BlogSettings" type="Fully.Qualified.TypeName.BlogSettings,   
      AssemblyName" />
  </configSections>
  <BlogSettings frontPagePostCount="10" title="You’ve Been Haacked" />
</configuration>

This is worth a read: 这值得一读:

http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM