C# 如何组合两个NameValueCollections?
我有两个NameValueCollections:C# 如何组合两个NameValueCollections?,c#,asp.net,collections,C#,Asp.net,Collections,我有两个NameValueCollections: NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings"); NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.
NameValueCollection customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
NameValueCollection appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
我尝试了customTag.Add(appSetting)代码>方法,但我得到以下错误:集合是只读的。
如何将它们合并为一个,以便可以同时访问两个集合中的所有元素?若要合并集合,请尝试以下操作:
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
集合访问组合设置
我尝试了customTag.Add(appSetting)代码>方法,但我得到以下错误:集合是只读的。
这意味着customTag
对象是只读的,不能写入.Add
尝试修改原始的NameValueCollection
System.Configuration
包含一个ReadOnlyNameValueCollection
,它扩展了NameValueCollection
,使其成为只读的,因此尽管强制转换为泛型NameValueCollection
,该对象仍然是只读的
如何将它们组合成一个元素,以便从两者访问所有元素
您只需将这两个集合添加到第三个可写的NameValueCollection
鉴于:
var customTag = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("secureAppSettings");
var appSetting = (NameValueCollection)System.Configuration.ConfigurationManager.GetSection("appSettings");
您可以。同时添加:
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”}
合并,结果将是{foo:[“bar”,“baz”]}
(为了简洁起见,使用JSON语法)。答案是完整的。但是,如果您想合并多个NAMEVALIONCORSECT或不修改任何原始的,则可以考虑以下方法:合并任意数量的<代码> NAMPATIONECOLSECTION/COMP> < < /P>
代码
/// <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
//
///将指定的NameValueCollection参数合并到单个NamedValueCollection中。
///
///要合并的NameValueCollection数组。
///合并的NameValueCollection
///
///如果传递的参数为null或空,则返回空集合。
///
静态NameValueCollection合并(参数NameValueCollection[]args)
{
NameValueCollection组合=新的NameValueCollection();
if(args==null | | args.Length==0)
{
综合收益;
}
NameValueCollection当前为空;
for(int i=0;i0)
{
合并。添加(当前);
}
}
综合收益;
}
示例用法
/// <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
NameValueCollection北美集合=新的NameValueCollection
{
{“卡纳达”,“渥太华”},
{“美国”、“华盛顿特区”},
{“墨西哥”、“墨西哥城”}
};
NameValueCollection南美集合=新的NameValueCollection
{
{“阿根廷”、“布宜诺斯艾利斯”},
{“秘鲁”、“利马”},
{“智利”、“圣地亚哥”}
};
NameValueCollection合并=合并(北美集合,null,南美集合,null);
for(int i=0;i
该方法还足够健壮,可以承受Merge(null)
或Merge()
,只需返回一个空的NameValueCollection
。这将确保您的代码不会因为输入错误而引发异常。如果它们都具有相同的键值,您想怎么办?它们应该覆盖。哪个应该覆盖?添加的应该覆盖原始值。在这种情况下,appSetting应该覆盖customTag。如果出现异常消息,您似乎必须创建一个新的、可写的NameValueCollection
,并将所有元素复制到其中。请注意,您必须自己迭代,调用NameValueCollection.Set()
方法,因为NameValueCollection
的默认行为是将添加的值附加到现有值的列表中,而不是覆盖它们。此方法如何处理类似数组的查询字符串参数?@palswim我没有专门测试过。你能给我举个不起作用的例子吗?如果你不想覆盖,而是合并/合并怎么办?嗨,@TimSchmelter。您可以使用Add方法合并NameValueCollections,而无需替换已有的集合。具有相同键的值组合为逗号分隔的字符串。请看合并方法中有趣的部分-重复键会发生什么情况?我想知道这一点!比公认的答案干净得多。