使用为 Web 方法实现 IDictionary 的内置对象的替代方法

发布于 2024-07-20 16:47:49 字数 198 浏览 4 评论 0 原文

我有一个使用在 ASP.NET 中创建的 Web 服务的 Web 应用程序。 在这个Web服务中,我想传递一个Key Value类型的集合对象(即Hashtable或Dictionay之类的东西)。

但我们不能使用从 IDictionary 实现的对象。

我不想在我的 Web 服务中创建序列化类。

谁能建议我最好的方法?

I have a web application that uses the Web Service created in ASP.NET. In this, web service I want to pass an collection object of Key Value type (i.e. something like Hashtable or Dictionay).

But we cannot use objects that implements from IDictionary.

I do not want to create a serialized class in my web service.

Can anyone suggest me the best approach for this?

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

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

发布评论

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

评论(5

孤独岁月 2024-07-27 16:47:49

dev.e.loper 几乎是对的。 您可以使用List

或者,您可以使用List>

MSDN 文档:

dev.e.loper is almost right. You can use a List<Pair>.

Alternatively, you can use List<KeyValuePair<TKey,TValue>>.

MSDN Documentation:

以往的大感动 2024-07-27 16:47:49

我不太清楚你的问题,但也许你需要这样的东西?

using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
        {
            return;
        }

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            var key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            var value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }

        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (var key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}

I'm not totally clear on your question, but maybe you are needing something like this?

using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    public XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(XmlReader reader)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
        {
            return;
        }

        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            var key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            var value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);
            reader.ReadEndElement();
            reader.MoveToContent();
        }

        reader.ReadEndElement();
    }

    public void WriteXml(XmlWriter writer)
    {
        var keySerializer = new XmlSerializer(typeof(TKey));
        var valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (var key in this.Keys)
        {
            writer.WriteStartElement("item");
            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();
            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}
遇见了你 2024-07-27 16:47:49

您可以继承可序列化的KeyedCollection。

http://msdn.microsoft.com/en-us/library/ms132438。 ASPX

You can inherit from KeyedCollection which is Serializable.

http://msdn.microsoft.com/en-us/library/ms132438.aspx

巴黎盛开的樱花 2024-07-27 16:47:49

我通过使用 DictionaryEntry 解决了这个问题,

唯一的区别是 Key 也是 Object。

我基本上有一个 Dictionary ToDictionary(DictionaryEntry[]条目) 和一个 DictionaryEntry[] FromDictionary(Dictionary Entry) 静态方法,它们非常轻量,最终让我到达同一个地方,而不必制作我自己的收藏类。

额外的好处是,结果产生的 XML 更接近 WCF Web 服务默认使用的 XML! 这意味着您现在可以在客户端代码中进行此更改,并为 WCF 做好准备(如果您决定这样做)。

JSON [{"Key": key1, "Value": value1}, {"Key": key2, "Value": value2}] 的结果与上面的结果完全相同默认为 WCF。

I solved this by using DictionaryEntry

The only difference is that Key is Object as well.

I basically have a Dictionary ToDictionary(DictionaryEntry[] entries) and a DictionaryEntry[] FromDictionary(Dictionary entries) static methods which are very light weight and end up getting me to the same place without having to make my own collection class.

The added benefit is that the XML which comes as a result is closer to that in which the WCF Web Services use by default! That means you can make this change now in your client code and be ready for WCF if you decide to move that way.

The result looks like this over JSON [{"Key": key1, "Value": value1}, {"Key": key2, "Value": value2}] exactly the same as it does over WCF by default.

×眷恋的温暖 2024-07-27 16:47:49

您可以尝试使用 2 个数组,1 个用于键,一个用于值,其中数组的索引匹配。 这不是最理想的解决方案,但却是一个有效的解决方案。 在 Web 服务的内部,您可以使用 IDictionary 并传递该对象的键和值。

You could try to use 2 arrays, 1 for keys and one for values, where the indexes of the arrays match up. Not the most ideal solution but a valid one. The internals of the webservice you can use IDictionary and just pass out the Keys and Values of that object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文