从NameValueCollection复制键值到泛型字典
本文关键字:泛型 字典 键值 NameValueCollection 复制 | 更新日期: 2023-09-27 18:13:25
试图将现有NameValueCollection对象中的值复制到Dictionary中。我有下面的代码来做到这一点,但似乎添加不接受我的键和值作为字符串
IDictionary<TKey, TValue> dict = new Dictionary<TKey, TValue>();
public void copyFromNameValueCollection (NameValueCollection a)
{
foreach (var k in a.AllKeys)
{
dict.Add(k, a[k]);
}
}
注意: NameValueCollection包含String键和值,所以我只是想在这里提供一个方法,允许将这些复制到通用字典。
扩展方法+ linq:
public static Dictionary<string, string> ToDictionary(this NameValueCollection nvc) {
return nvc.AllKeys.ToDictionary(k => k, k => nvc[k]);
}
//example
var dictionary = nvc.ToDictionary();
在这里使用泛型是没有意义的,因为你不能将string
s分配给任意的泛型类型:
IDictionary<string, string> dict = new Dictionary<string, string>();
public void copyFrom(NameValueCollection a)
{
foreach (var k in a.AllKeys)
{
dict.Add(k, a[k]);
}
}
尽管你可能应该创建一个方法来创建一个新的字典:
public static IDictionary<string, string> ToDictionary(this NameValueCollection col)
{
IDictionary<string, string> dict = new Dictionary<string, string>();
foreach (var k in col.AllKeys)
{
dict.Add(k, col[k]);
}
return dict;
}
可以这样使用:
NameValueCollection nvc = //
var dictionary = nvc.ToDictionary();
如果您需要将集合中的字符串转换为所需的键/值类型的通用方法,您可以使用类型转换器:
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this NameValueCollection col)
{
var dict = new Dictionary<TKey, TValue>();
var keyConverter = TypeDescriptor.GetConverter(typeof(TKey));
var valueConverter = TypeDescriptor.GetConverter(typeof(TValue));
foreach(string name in col)
{
TKey key = (TKey)keyConverter.ConvertFromString(name);
TValue value = (TValue)valueConverter.ConvertFromString(col[name]);
dict.Add(key, value);
}
return dict;
}
parameters.AllKeys.ToDictionary(t => t, t => parameters[t]);
使用LINQ:
public static IDictionary<string, string> ToDictionary(this NameValueCollection collection)
{
return collection.Cast<string>().ToDictionary(k => k, v => collection[v]);
}
用法:
IDictionary<string, string> dic = nv.ToDictionary();
超短版
var dataNvc = HttpUtility.ParseQueryString(data);
var dataCollection = dataNvc.AllKeys.ToDictionary(o => o, o => dataNvc[o]);
如果你知道你的字典总是包含字符串,指定它包含字符串,而不是使你的类泛型:
IDictionary<string, string> dict = new Dictionary<string, string>();
有了这个,事情就会像写的那样"正常工作"(没有泛型方法规范)。
如果你需要这是一个泛型类,并持有泛型数据,你需要一些方法从string
转换到TKey
和string
到TValue
。您可以为您的copy方法提供委托来执行此操作:
public void CopyFrom(NameValueCollection a, Func<string, TKey> keyConvert, Func<string, TValue> valueConvert)
{
foreach(var k in a.AllKeys)
{
dict.Add(keyConvert(k), valueConvert(a[k]));
}
}
然后你需要传递一个委托,它将执行从字符串到TValue
和字符串到TKey
的转换。
你不应该忘记EqualityComparer。但它不是公共财产。因此,您应该使用反射来获取它。
public static IEqualityComparer GetEqualityComparer(this NameObjectCollectionBase nameObjectCollection)
{
PropertyInfo propertyInfo = typeof(NameObjectCollectionBase).GetProperty("Comparer", BindingFlags.Instance | BindingFlags.NonPublic);
return (IEqualityComparer)propertyInfo.GetValue(nameObjectCollection);
}
public static IEqualityComparer<string> GetEqualityComparer(this NameValueCollection nameValueCollection)
{
return (IEqualityComparer<string>)((NameObjectCollectionBase)nameValueCollection).GetEqualityComparer();
}
public static Dictionary<string, string> ToDictionary(this NameValueCollection nameValueCollection)
{
Dictionary<string, string> dictionary =
nameValueCollection.AllKeys.ToDictionary(x => x, x => nameValueCollection[x], nameValueCollection.GetEqualityComparer());
return dictionary;
}