在可序列化字典上使用 foreach 语句
我有一个为 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字典提供键/值查找是有原因的,它不是您通常会迭代的东西。您的类所包装的通用
Dictionary<>
对象已经有一个名为ContainsKey()
的方法,该方法将完全执行您正在寻找的操作,而无需额外的开销通过每个键/值对来查看它是否存在。无需公开迭代器,只需将其添加到您的类中即可。并这样称呼它。
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 calledContainsKey()
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.And call it like this.
您的类需要实现 IEnumerable 来对字典的键执行 foreach,因此您的类将如下所示
然后您需要从 IEnumerable 接口实现以下方法,如下所示:
然后,当您执行 foreach 时,将从字典中获取键因此你可以让你的 foreach 如下所示:
Your class needs to implement IEnumerable to perform foreach on the Key of the dictionary, hence your class would look as below
Then you need to implement the following methods from the IEnumerable interface as shown:
Then when you perform a foreach the keys from dictionary are fetched hence you can have your foreach as shown:
如果您实际上需要迭代字典(而不仅仅是检查某个键是否存在),Dictionary.Keys 属性将返回一个可枚举集合,您可以使用它迭代字典:
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: