mik*_*nne 14 .net configuration app-config config web-config
有没有办法让我在ConfigurationSection的自定义子类中包含一个简单的字符串数组或List <string>?(或者简单数据对象的数组或通用列表,就此而言?)
我已经熟悉新的(和非常详细的)ConfigurationSection,ConfigurationElement和ConfigurationElementCollection类,但我还不是专家.
似乎ConfigurationSection应该自己处理简单的集合/列表,而不必为每一个创建自定义的ConfigurationElementCollection子类.但是我没有在网上找到任何关于这种能力的提法.
编辑:接受Dan的回答作为答案,因为它可能是我最接近"旧式"configSections的东西.我总是发现任何XmlSerializable对象都可以很容易地成为一个configSection,它简单,灵活,优雅.我确信新框架更强大; 然而令人遗憾的是,对于简单的配置结构来说,这很麻烦,我们已经减少了回到String.Split().
gra*_*der 19
这是一个简单的例子.
//START CODE
//MyCompany.MyProject.csproj which results in MyCompany.MyProject.dll
//Add a Folder called "Configuration"
namespace MyCompany.MyProject.Configuration
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
public class TransformationToDirectoryMapping : ConfigurationElement
{
private const string FRIENDLY_NAME = "FriendlyName";
private const string PICKUP_FOLDER = "PickupFolder";
[ConfigurationProperty(FRIENDLY_NAME, DefaultValue = "", IsKey = false, IsRequired = true)]
public string FriendlyName
{
get
{
return ((string)(base[FRIENDLY_NAME]));
}
set
{
base[FRIENDLY_NAME] = value;
}
}
[ConfigurationProperty(PICKUP_FOLDER, DefaultValue = "", IsKey = true, IsRequired = true)]
public string PickupFolder
{
get
{
return ((string)(base[PICKUP_FOLDER]));
}
set
{
base[PICKUP_FOLDER] = value;
}
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
[ConfigurationCollection(typeof(TransformationToDirectoryMapping))]
public class TransformationToDirectoryMappingCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TransformationToDirectoryMapping();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TransformationToDirectoryMapping)(element)).PickupFolder;
}
public TransformationToDirectoryMapping this[int idx]
{
get
{
return (TransformationToDirectoryMapping)BaseGet(idx);
}
}
new public TransformationToDirectoryMapping this[string key]
{
get
{
return (TransformationToDirectoryMapping)BaseGet(key);
}
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public class TransformationToDirectoryMappingConfigSection : ConfigurationSection
{
private const string TRANSFORMATION_TO_DIRECTORY_MAPPINGS = "TransformationToDirectoryMappings";
[ConfigurationProperty(TRANSFORMATION_TO_DIRECTORY_MAPPINGS)]
public TransformationToDirectoryMappingCollection TransformationToDirectoryMappingItems
{
get { return ((TransformationToDirectoryMappingCollection)(base[TRANSFORMATION_TO_DIRECTORY_MAPPINGS])); }
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public static class MyRetriever
{
public const string MAPPINGS_CONFIGURATION_SECTION_NAME = "TransformationToDirectoryMappingsSection";
public static TransformationToDirectoryMappingCollection GetTheCollection()
{
TransformationToDirectoryMappingConfigSection mappingsSection = (TransformationToDirectoryMappingConfigSection)ConfigurationManager.GetSection(MAPPINGS_CONFIGURATION_SECTION_NAME);
if (mappingsSection != null)
{
return mappingsSection.TransformationToDirectoryMappingItems;
}
return null; // OOPS!
}
}
}
Run Code Online (Sandbox Code Playgroud)
//配置文件的XML:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="TransformationToDirectoryMappingsSection" type="MyCompany.MyProject.Configuration.TransformationToDirectoryMappingConfigSection, MyCompany.MyProject"/>
</configSections>
<TransformationToDirectoryMappingsSection>
<TransformationToDirectoryMappings>
<add FriendlyName="Hello" PickupFolder="C:\WUWUTemp\pickups\pickup11\" />
<add FriendlyName="GoodBye" PickupFolder="C:\WUWUTemp\pickups\pickup12\" />
</TransformationToDirectoryMappings>
</TransformationToDirectoryMappingsSection>
</configuration>
Run Code Online (Sandbox Code Playgroud)
gra*_*der 11
应用程序设置架构
http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx
好的,一个旧帖子,但我记得当我遇到类似情况时:
...
如果您转到项目/项目属性(在VS2008或VS2010中).有一个"设置"标签.
如果你添加一个新值....
其中一种类型称为:System.Collections.Specialized.StringCollection
给它一个名字(我使用"FavoriteColors").
设置类型(如上所述).
设置值.
"字符串集合编辑器"说"输入集合中的字符串(每行一个)".
我进入了:
红色
黄色
黑色
白色
这将为您的app.config文件添加一些xml.
<setting name="FavoriteColors" serializeAs="Xml">
<value>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>red</string>
<string>yellow</string>
<string>black</string>
<string>white</string>
</ArrayOfString>
</value>
</setting>
Run Code Online (Sandbox Code Playgroud)
(你会更好地完成这些步骤,而不是粘贴上面的xml,因为(为了简洁)我没有将所有xml添加到生成的这篇文章中.
您应该能够通过以下代码"获取"值:
private void ShowMyFavoriteColors()
{
Properties.Settings.Default.FavoriteColors.Cast<string>().ToList().ForEach(myfavcolor =>
{
string temp = myfavcolor;
});
}
Run Code Online (Sandbox Code Playgroud)
注意,上面的步骤将生成以下C#代码(为您创建的自动代码....它不是您创建的代码)但代码如下所示:
[global::System.Configuration.ApplicationScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute(@"<?xml version=""1.0"" encoding=""utf-16""?>
<ArrayOfString xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<string>red</string>
<string>yellow</string>
<string>black</string>
<string>white</string>
</ArrayOfString>")]
public global::System.Collections.Specialized.StringCollection FavoriteColors {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["FavoriteColors"]));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
好的,你要求简单.那么,存储一系列字符串的最简单方法是使用分隔列表(例如,逗号分隔).这样你就可以将它存储在一个元素中(例如在appSettings中).
<add key="weekDays" value="Monday,Tuesday,Wednesday,Thursday,Friday"/>
Run Code Online (Sandbox Code Playgroud)
当然,这有缺点但在大多数情况下适用于简单列表.然后,您可以使用String.Split()将其转换回数组/列表.
否则你回到ConfigurationSection元素,我同意这些元素非常冗长和笨拙.但我不知道其他任何方式,我害怕(但我很高兴被证明是错的!).
| 归档时间: |
|
| 查看次数: |
18531 次 |
| 最近记录: |