插入到 .NET 中的 NameValueCollection

发布于 2024-08-15 09:37:40 字数 64 浏览 6 评论 0原文

是否可以将项目插入到特定索引中的 NameValueCollection 中?我没有看到 Insert() 方法。

Is it possible to insert an item into a NameValueCollection in a specific index? I don't see an Insert() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱影子 2024-08-22 09:37:40

以下是 NameValueCollectionInsert 扩展方法的实现:

static class ColExtensions
{
    private class KeyValuesPair
    {
        public string Name { get; set; }
        public string[] Values { get; set; }
    }

    public static void Insert(this NameValueCollection col, int index, string name, string value)
    {
        if (index < 0 || index > col.Count)
            throw new ArgumentOutOfRangeException();

        if (col.GetKey(index) == value)
        {
            col.Add(name, value);
        }
        else
        {
            List<KeyValuesPair> items = new List<KeyValuesPair>();
            int size = col.Count;
            for (int i = index; i < size; i++)
            {
                string key = col.GetKey(index);
                items.Add(new KeyValuesPair
                {
                    Name = key,
                    Values = col.GetValues(index),
                });
                col.Remove(key);
            }

            col.Add(name, value);

            foreach (var item in items)
            {
                foreach (var v in item.Values)
                {
                    col.Add(item.Name, v);
                }
            }
        }
    }
}

Here's an implementation of an Insert extension method for NameValueCollection:

static class ColExtensions
{
    private class KeyValuesPair
    {
        public string Name { get; set; }
        public string[] Values { get; set; }
    }

    public static void Insert(this NameValueCollection col, int index, string name, string value)
    {
        if (index < 0 || index > col.Count)
            throw new ArgumentOutOfRangeException();

        if (col.GetKey(index) == value)
        {
            col.Add(name, value);
        }
        else
        {
            List<KeyValuesPair> items = new List<KeyValuesPair>();
            int size = col.Count;
            for (int i = index; i < size; i++)
            {
                string key = col.GetKey(index);
                items.Add(new KeyValuesPair
                {
                    Name = key,
                    Values = col.GetValues(index),
                });
                col.Remove(key);
            }

            col.Add(name, value);

            foreach (var item in items)
            {
                foreach (var v in item.Values)
                {
                    col.Add(item.Name, v);
                }
            }
        }
    }
}
拒绝两难 2024-08-22 09:37:40

不,没有方法可以在特定索引处插入项目。但是,为此编写一个扩展方法应该很简单。

您使用 NameValueCollection 的原因是什么?

No, there is no method to insert an item at a specific index. However, it should be trivial to write a extension method for doing that.

What's the reason you're using a NameValueCollection?

辞取 2024-08-22 09:37:40

这并不是 NameValueCollection 的真正用途;如果您需要以这种方式操作您的集合,您应该考虑不同的数据结构(OrderedDictionary?)。也就是说,这里有一个扩展方法可以完成您想要的操作:

static class NameValueCollectionExtensions {
    public static void Insert(this NameValueCollection collection, int index, string key, string value) {
        int count = collection.Count;
        if (index < 0 || index > count) {
            throw new ArgumentOutOfRangeException("index");
        }
        List<string> keys = new List<string>(collection.AllKeys);
        List<string> values = keys.Select(k => collection[k]).ToList();
        keys.Insert(index, key);
        values.Insert(index, value);
        collection.Clear();
        for (int i = 0; i <= count; i++) {
            collection.Add(keys[i], values[i]);
        }
    }
}

我不知道 collection.AllKeys 是否保证按照插入的顺序返回键。如果您可以找到说明这种情况的文档,那么上面的内容就可以了。否则,将该行替换

List<string> keys = new List<string>(collection.AllKeys);

List<string> keys = new List<string>();
for(int i = 0; i < count; i++) {
    keys.Add(collection.Keys[i]);
}

This is not really what NameValueCollections are meant for; if you need to manipulate your collection in this way you should consider a different data structure (OrderedDictionary?). That said, here's an extension method that will do what you want:

static class NameValueCollectionExtensions {
    public static void Insert(this NameValueCollection collection, int index, string key, string value) {
        int count = collection.Count;
        if (index < 0 || index > count) {
            throw new ArgumentOutOfRangeException("index");
        }
        List<string> keys = new List<string>(collection.AllKeys);
        List<string> values = keys.Select(k => collection[k]).ToList();
        keys.Insert(index, key);
        values.Insert(index, value);
        collection.Clear();
        for (int i = 0; i <= count; i++) {
            collection.Add(keys[i], values[i]);
        }
    }
}

I don't know that collection.AllKeys is guaranteed to return the keys in the order that they were inserted. If you can find documentation stating that this is the case then the above is fine. Otherwise, replace the line

List<string> keys = new List<string>(collection.AllKeys);

with

List<string> keys = new List<string>();
for(int i = 0; i < count; i++) {
    keys.Add(collection.Keys[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文