在可序列化字典上使用 foreach 语句

发布于 2024-12-18 17:47:16 字数 1437 浏览 1 评论 0原文

我有一个为 WCF REST Web 服务创建的可序列化字典,

[Serializable]
public class jsonDictionary<TKey, TValue> : ISerializable
{
    private Dictionary<TKey, TValue> _Dictionary;
    public jsonDictionary()
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public jsonDictionary(SerializationInfo info, StreamingContext context)
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public TValue this[TKey key]
    {
        get { return _Dictionary[key]; }
        set { _Dictionary[key] = value; }
    }
    public void Add(TKey key, TValue value)
    {
        _Dictionary.Add(key, value);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (TKey key in _Dictionary.Keys)
            info.AddValue(key.ToString(), _Dictionary[key]);
    }
}

我需要搜索该字典以确定是否存在多个可能的键之一。我想我会使用类似这样的 foreach 语句来做到这一点

foreach(var pair in dictionary)
{
    switch(pair.key)
    {
        case "something":

        Break;
        case "somethingelse":
        Break;
     }
}

但是我不断收到错误:

    foreach statement cannot operate on variables of type 'ZTERest.jsonDictionary<string,string>' because 'ZTERest.jsonDictionary<string,string>' does not contain a public definition for 'GetEnumerator'

我知道我必须使用 IEnumerable 或 IEnumerator 接口做一些事情,但我不确定如何做。

I have a serializable dictionary that I created for a WCF REST web service

[Serializable]
public class jsonDictionary<TKey, TValue> : ISerializable
{
    private Dictionary<TKey, TValue> _Dictionary;
    public jsonDictionary()
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public jsonDictionary(SerializationInfo info, StreamingContext context)
    {
        _Dictionary = new Dictionary<TKey, TValue>();
    }
    public TValue this[TKey key]
    {
        get { return _Dictionary[key]; }
        set { _Dictionary[key] = value; }
    }
    public void Add(TKey key, TValue value)
    {
        _Dictionary.Add(key, value);
    }
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        foreach (TKey key in _Dictionary.Keys)
            info.AddValue(key.ToString(), _Dictionary[key]);
    }
}

I need to search through this dictionary to determine if 1 of several possible keys are present. I figured I would do this using a foreach statement kinda like so

foreach(var pair in dictionary)
{
    switch(pair.key)
    {
        case "something":

        Break;
        case "somethingelse":
        Break;
     }
}

However I keep getting the error:

    foreach statement cannot operate on variables of type 'ZTERest.jsonDictionary<string,string>' because 'ZTERest.jsonDictionary<string,string>' does not contain a public definition for 'GetEnumerator'

I know I have to do something with the IEnumerable or IEnumerator interface but I'm not sure how.

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

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

发布评论

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

评论(3

素衣风尘叹 2024-12-25 17:47:16

字典提供键/值查找是有原因的,它不是您通常会迭代的东西。您的类所包装的通用 Dictionary<> 对象已经有一个名为 ContainsKey() 的方法,该方法将完全执行您正在寻找的操作,而无需额外的开销通过每个键/值对来查看它是否存在。无需公开迭代器,只需将其添加到您的类中即可。

public bool ContainsKey(TKey key)
{
    return _Dictionary.ContainsKey(key);
}

并这样称呼它。

if (dictionary.ContainsKey("Something"))
{
    //do something
}

A dictionary provides a key/value lookup for a reason, it's not something that you usually iterate through. The generic Dictionary<> object that your class is wrapping already has a method called ContainsKey() that will do exactly what you're looking for, without the overhead of going through every single key/value pair to see if it's there. There's no need to expose an iterator, just add this to your class.

public bool ContainsKey(TKey key)
{
    return _Dictionary.ContainsKey(key);
}

And call it like this.

if (dictionary.ContainsKey("Something"))
{
    //do something
}
皓月长歌 2024-12-25 17:47:16

您的类需要实现 IEnumerable 来对字典的键执行 foreach,因此您的类将如下所示

public class jsonDictionary<TKey, TValue> : ISerializable, IEnumerable<TKey>

然后您需要从 IEnumerable 接口实现以下方法,如下所示:

public IEnumerator<TKey> GetEnumerator()
{
    // Since we want to iterate on the Key we specifiying the enumerator on keys
    return _Dictionary.Keys.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

然后,当您执行 foreach 时,将从字典中获取键因此你可以让你的 foreach 如下所示:

foreach(var key in dictionary)
{
    switch(key)
    {
         case "Something":
             // do something
             break;
         case "SomethingElse":
             //do something
             break;
    }
}

Your class needs to implement IEnumerable to perform foreach on the Key of the dictionary, hence your class would look as below

public class jsonDictionary<TKey, TValue> : ISerializable, IEnumerable<TKey>

Then you need to implement the following methods from the IEnumerable interface as shown:

public IEnumerator<TKey> GetEnumerator()
{
    // Since we want to iterate on the Key we specifiying the enumerator on keys
    return _Dictionary.Keys.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
    return GetEnumerator();
}

Then when you perform a foreach the keys from dictionary are fetched hence you can have your foreach as shown:

foreach(var key in dictionary)
{
    switch(key)
    {
         case "Something":
             // do something
             break;
         case "SomethingElse":
             //do something
             break;
    }
}
誰認得朕 2024-12-25 17:47:16

如果您实际上需要迭代字典(而不仅仅是检查某个键是否存在),Dictionary.Keys 属性将返回一个可枚举集合,您可以使用它迭代字典:

foreach(var key in dictionary.Keys)
{
    if(dictionary[key] == "something")
    {
        // Do something
    }
}

In case you actually need to iterate through the dictionary (instead of just checking if a certain key exists) the Dictionary.Keys property returns an enumerable collection which you could use iterate through the dictionary:

foreach(var key in dictionary.Keys)
{
    if(dictionary[key] == "something")
    {
        // Do something
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文