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
构造有内部调用Add
的缩写:
var collection = new NameValueCollection(customTag);
collection.Add(appSettings);
请注意,在这两种情况下,使用Add
都会允许将多个值添加到每个键。
例如,如果您要合并{foo: "bar"}
和{foo: "baz"}
,结果将为{foo: ["bar", "baz"]}
(为简洁起见,使用JSON语法)。
如果他们都具有相同的键值,你想要做什么? – 2015-01-20 21:41:35
他们应该覆盖。 – User765876 2015-01-20 21:42:28
哪个应该覆盖? – 2015-01-20 21:45:13