简体   繁体   English

.Net 中 NameValueCollection 的通用形式

[英]Generic form of NameValueCollection in .Net

Does .Net provides generic form of NameValueCollection or an alternative to Dictionary<string,List<T>> ? .Net 是否提供通用形式的 NameValueCollection 或Dictionary<string,List<T>>的替代品?

Something like就像是

Person john = new Person();
...
Person vick = new Person();
...

NameValueCollection<Person> stringToPerson = new  NameValueCollection<Person>();
stringToPerson.Add("John",john)
stringToPerson.Add("Vick",vick)

Actually in my case am forced to rely on Dictionary<string,List<Peron>> , is there any other alternative?实际上在我的情况下被迫依赖Dictionary<string,List<Peron>> ,还有其他选择吗?

Regards, Jeez问候, 吉兹

The closest alternative is probably the ILookup<TKey, TElement> interface. 最接近的替代方案可能是ILookup<TKey, TElement>接口。 At the moment, the only public type that implements it in the BCL is the immutable Lookup<TKey, TElement> class, an instance of which can be created with the Enumerable.ToLookup method. 目前,在BCL中实现它的唯一公共类型是不可变的Lookup<TKey, TElement>类,其实例可以使用Enumerable.ToLookup方法创建。 If you want a mutable type that implements the interface, you'll have to write one yourself; 如果你想要一个实现接口的可变类型,你必须自己写一个; you can find an example implementation here . 你可以在这里找到一个示例实现。

In your case, you probably want an ILookup<string, Person> . 在你的情况下,你可能想要一个ILookup<string, Person>

There's no such thing built in to the BCL as far as I know. 据我所知,BCL没有内置这样的东西。 I would just write your own class which wraps a Dictionary<string, List<T>> internally and exposes appropriate methods (eg, Add could add an element to the List<T> for the given key). 我只想编写你自己的类,它在内部包含Dictionary<string, List<T>>并公开适当的方法(例如, Add可以为给定键的List<T>添加一个元素)。

For example: 例如:

class NameValueCollection<T>
{
    Dictionary<string, List<T>> _dict = new Dictionary<string, List<T>>();

    public void Add(string name, T value)
    {
        List<T> list;
        if (!_dict.TryGetValue(name, out list))
        {
            _dict[name] = list = new List<T>();
        }

        list.Add(value);
    }

    // etc.
}

Oh, I see what you want to do now. 哦,我明白你现在想做什么。 You want to be able to add to the Person collection without having to create a new List each time. 您希望能够添加到Person集合,而无需每次都创建新的List。 Extension methods to the rescue! 救援的延伸方法!

public static void SafeAdd<TValue>(this IDictionary<TKey, ICollection<TValue>> dict, TKey key, TValue value)
{
     HashSet<T> container;
     if (!dict.TryGetValue(key, out container)) 
     {
         dict[key] = new HashSet<TValue>();
     }
     dict[key].Add(value);
}

Usage:
var names = new Dictionary<string, ICollection<Person>>();
names.SafeAdd("John", new Person("John"));

Nothing inbuilt; 什么都没有内置; there is Lookup<TKey,TValue> which operates as a multi-map, but that is immutable. Lookup<TKey,TValue>作为多地图运行,但这是不可变的。 I wrote a mutable EditableLookup<TKey,TValue> for MiscUtil which may help. 我为MiscUtil写了一个可变的EditableLookup<TKey,TValue> ,这可能有所帮助。

Here is a C# example of complete implementation of the generic NameValueCollection based on NameObjectCollectionBase .是基于NameObjectCollectionBase的通用NameValueCollection的完整实现的 C# 示例。 What makes this collection special unlike Dictionary , is that one key can contain several elements.Dictionary不同的是,这个集合的特别之处在于一个键可以包含多个元素。

Usage:用法:

NameValueCollection<int> collection = new NameValueCollection<int>();
collection.Add("a", 123);
collection.Add("a", 456); // 123 and 456 will be inserted into the same key. 
collection.Add("b", 789); // 789 will be inserted into another key.

int[] a = collection.Get("a"); // contains 132 and 456.
int[] b = collection.Get("b"); // contains 789.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.Net和NameValueCollection - ASP.Net and NameValueCollection 如何在C#(asp.net)中对使用Request.Form创建NameValueCollection的方法进行单元测试 - How to unit test method that creates NameValueCollection using Request.Form in C# (asp.net) 将键值从NameValueCollection复制到Generic Dictionary - Copy key values from NameValueCollection to Generic Dictionary .NET获取自定义配置部分作为NameValueCollection - .NET get custom configuration section as NameValueCollection 在NameValueCollection C#.net中编码值 - Encode values in a NameValueCollection C# .net 是否存在与哈希表或NameValueCollection通用的等效项,该哈希表或NameValueCollection允许使用同一键的多个值? - Is there a generic equivalent to a hash table, or NameValueCollection that allows multiple values with the same key? this.QueryString(NameValueCollection)从另一个类访问Form1 - this.QueryString (NameValueCollection) access Form1 - from another class Form.Post 中的 NameValueCollection 显示 FIELDS 和 VALUES - NameValueCollection from Form.Post show FIELDS and VALUES .net配置文件AppSettings:NameValueCollection与KeyValueConfigurationCollection - .net config file AppSettings: NameValueCollection vs. KeyValueConfigurationCollection 如何使用Quartz.Net中的NameValueCollection访问任何作业属性? - how to access any job properties using NameValueCollection in Quartz.Net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM