C# AppSettings:是否有一种简单的方法可以将集合放入 <appSetting>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1755421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# AppSettings: Is there a easy way to put a collection into <appSetting>
提问by Markus
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?
但我只得到最后一个成员。我怎么能轻松解决这个问题?
采纳答案by Ashish Jain
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.
在这种情况下,List(类似于我的 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
配置元素
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
配置元素集合
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
网页配置部分
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;
}
}
}
回答by Phil.Wheeler
You'd likely be better off putting this information in a separate XML file and having a reference to that file in AppSettings. That would give you a lot more flexibility around how you retrieved the information and consumed it.
最好将此信息放在单独的 XML 文件中,并在 AppSettings 中引用该文件。这将为您在如何检索和使用信息方面提供更大的灵活性。
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/comma/semi-colon] 分隔数组.
回答by ChirpingPanda
Haacked provides a concise approach to configuration settings. His approach uses one class deriving from ConfigurationSection. So for his blog example your app.config or web.config xml representation will look like this:
Haacked 提供了一种简洁的配置设置方法。他的方法使用派生自 ConfigurationSection 的一个类。因此,对于他的博客示例,您的 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
回答by Bill H
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]);
}
回答by Graeme Christie
NinjaSettingsdoes 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. Generally you would wire this up using IOC, but the basic usage is
然后访问您的设置用户 NinjaSettings 包装器。通常你会使用 IOC 连接它,但基本用法是
var settings = new NinjaSettings<IAppSettings>().Settings;
int total = 0;
for (var i in settings.List)
{
total+=i;
}