如何在 ConfigurationSection 中包含簡單集合
有沒有辦法讓我在我的 ConfigurationSection 的自定義子類中包含一個簡單的字元串數組或 List<string>?(或者是簡單數據對象的數組或通用列表?)
我對新的(非常冗長的)ConfigurationSection、ConfigurationElement 和 ConfigurationElementCollection 類越來越熟悉,但我還不是專家。
似乎 ConfigurationSection 應該自己處理簡單的集合/列表,而無需我為每一個創建自定義 ConfigurationElementCollection 子類。但是我在網上沒有找到任何關於這種能力的參考。
**編輯:**接受 Dan 的回答作為答案,因為這可能是我最接近“舊式”configSections 的東西。我總是發現任何 XmlSerializable 對像都可以輕鬆成為 configSection,這很容易、靈活且優雅。我確信新框架更強大;但是很遺憾,對於簡單的配置結構來說它是如此繁瑣,以至於我們不得不回到 String.Split()。
這是一個簡單的例子。
//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! } } }//配置文件的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>
應用程序設置架構
<http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx>
好的,一個舊文章,但是當我遇到類似情況時我記得它:
…
如果您轉到項目/項目屬性(在 VS2008 或 VS2010 中)。有一個“設置”選項卡。
如果添加新值….
其中一種類型稱為:System.Collections.Specialized.StringCollection
給它起個名字(我用的是“FavoriteColors”)。
設置類型(如上所示)。
設置值。
“字元串集合編輯器”顯示“在集合中輸入字元串(每行一個)”。
我進入了:
紅色的
黃色的
黑色的
白色的
這會將一些 xml 添加到您的 app.config 文件中。
<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>(您最好完成這些步驟,而不是粘貼上面的 xml,因為(為簡潔起見)我沒有將所有 xml 添加到生成的這篇文章中。
您應該能夠通過如下程式碼“獲取”這些值:
private void ShowMyFavoriteColors() { Properties.Settings.Default.FavoriteColors.Cast<string>().ToList().ForEach(myfavcolor => { string temp = myfavcolor; }); }請注意,上述步驟將生成以下 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"])); } } } }