我有两个NameValueCollections:
NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
我尝试了customTag.Add(appSetting);
方法,但收到此错误:Collection is read-only.
我如何将它们组合成一个,所以我可以访问两者中的所有元素?
答案 0 :(得分:7)
要合并集合,请尝试以下操作:
var secureSettings = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
var appSettings = (NameValueCollection)System.Configuration.ConfigurationManager.AppSettings;
// Initialise a new NameValueCollection with the contents of the secureAppSettings section
var allSettings = new NameValueCollection(secureSettings);
// Add the values from the appSettings section
foreach (string key in appSettings)
{
// Overwrite any entry already there
allSettings[key] = appSettings[key];
}
使用新的allSettings
集合来访问组合设置。
答案 1 :(得分:1)
我尝试了
customTag.Add(appSetting);
方法,但收到此错误:Collection is read-only.
这意味着customTag
对象是只读的,无法写入。 .Add
尝试修改原始NameValueCollection
。 System.Configuration
包含一个扩展ReadOnlyNameValueCollection
的{{1}}以使其只读,因此尽管转换为通用NameValueCollection
,该对象仍然是只读的。
我如何将它们组合成一个,所以我可以访问两者中的所有元素?
您只需将两个集合添加到第三个可写NameValueCollection
。
假设:
NameValueCollection
你可以var customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
var appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
:
.Add
但是,var collection = new NameValueCollection();
collection.Add(customTag);
collection.Add(appSettings);
构造函数有一个在内部调用NameValueCollection
的简写:
Add
请注意,在这两种情况下,使用var collection = new NameValueCollection(customTag);
collection.Add(appSettings);
都会允许将多个值添加到每个键中。
例如,如果要合并Add
和{foo: "bar"}
,结果将为{foo: "baz"}
(JSON语法用于简洁)。
答案 2 :(得分:0)
zzzzBov的答案已完成。但是,如果要合并多个NameValueCollection或不修改任何原始NameValueCollection,可以考虑以下方法合并任意数量的NameValueCollection
。
<强>代码强>
/// <summary>
/// Merges the specified NameValueCollection arguments into a single NamedValueCollection.
/// </summary>
/// <param name="args">An array of NameValueCollection to be merged.</param>
/// <returns>Merged NameValueCollection</returns>
/// <remarks>
/// Returns an empty collection if args passed are null or empty.
/// </remarks>
static NameValueCollection Merge(params NameValueCollection[] args)
{
NameValueCollection combined = new NameValueCollection();
if (args == null || args.Length == 0)
{
return combined;
}
NameValueCollection current = null;
for (int i = 0; i < args.Length; i++)
{
current = args[i];
if (current != null && current.Count > 0)
{
combined.Add(current);
}
}
return combined;
}
使用示例
NameValueCollection northAmericaCollection = new NameValueCollection
{
{ "Cananda", "Ottawa" },
{ "USA", "Washington, D.C." },
{ "Mexico", "Mexico City" }
};
NameValueCollection southAmericaCollection = new NameValueCollection
{
{ "Argentina", "Buenos Aires" },
{ "Peru", "Lima" },
{ "Chile", "Santiago" }
};
NameValueCollection merged = Merge(northAmericaCollection, null, southAmericaCollection, null);
for (int i = 0; i < merged.Count; i++)
{
Console.WriteLine("Name: {0}, Value: {1}", merged.GetKey(i), merged.Get(i));
}
// The example displays the following output:
// Name: Cananda, Value: Ottawa
// Name: USA, Value: Washington, D.C.
// Name: Mexico, Value: Mexico City
// Name: Argentina, Value: Buenos Aires
// Name: Peru, Value: Lima
// Name: Chile, Value: Santiago
该方法也足够强大,可以承受Merge(null)
或Merge()
,只需返回空NameValueCollection
。这将确保您的代码不会因输入错误而抛出异常。