如何合并两个 NameValueCollection?
How to combine two NameValueCollections?
我有两个 NameValueCollections:
NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
我尝试了 customTag.Add(appSetting);
方法,但出现此错误:Collection is read-only.
我如何将它们合并为一个,以便我可以访问两者的所有元素?
要组合这些集合,请尝试以下操作:
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
集合访问组合设置。
I tried customTag.Add(appSetting);
method but I get this error: Collection is read-only.
这意味着 customTag
对象是只读的,不能写入。 .Add
尝试修改原始 NameValueCollection
。 System.Configuration
包含一个 ReadOnlyNameValueCollection
扩展 NameValueCollection
以使其成为只读的,因此尽管转换为通用 NameValueCollection
,该对象仍然是只读的。
How would I combine them to one, so i can access all the elements from both?
您只需将这两个集合添加到第三个可写集合 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
构造函数有一个 shorthand 在内部调用 Add
:
var collection = new NameValueCollection(customTag);
collection.Add(appSettings);
请注意,在这两种情况下,使用 Add
将允许向每个键添加多个值。
例如,如果您要合并 {foo: "bar"}
和 {foo: "baz"}
,结果将是 {foo: ["bar", "baz"]}
(JSON 语法用于简洁)。
的回答已完成。但是,如果你想合并多个 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()
,并且只需 return 一个空的 NameValueCollection
。这将确保您的代码不会因为输入错误而引发异常。
我有两个 NameValueCollections:
NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
我尝试了 customTag.Add(appSetting);
方法,但出现此错误:Collection is read-only.
我如何将它们合并为一个,以便我可以访问两者的所有元素?
要组合这些集合,请尝试以下操作:
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
集合访问组合设置。
I tried
customTag.Add(appSetting);
method but I get this error:Collection is read-only.
这意味着 customTag
对象是只读的,不能写入。 .Add
尝试修改原始 NameValueCollection
。 System.Configuration
包含一个 ReadOnlyNameValueCollection
扩展 NameValueCollection
以使其成为只读的,因此尽管转换为通用 NameValueCollection
,该对象仍然是只读的。
How would I combine them to one, so i can access all the elements from both?
您只需将这两个集合添加到第三个可写集合 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
构造函数有一个 shorthand 在内部调用 Add
:
var collection = new NameValueCollection(customTag);
collection.Add(appSettings);
请注意,在这两种情况下,使用 Add
将允许向每个键添加多个值。
例如,如果您要合并 {foo: "bar"}
和 {foo: "baz"}
,结果将是 {foo: ["bar", "baz"]}
(JSON 语法用于简洁)。
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()
,并且只需 return 一个空的 NameValueCollection
。这将确保您的代码不会因为输入错误而引发异常。