简体   繁体   English

在包装字典类中实现IEnumerable.GetEnumerator()

[英]Implementing IEnumerable.GetEnumerator() in a wrapper dictionary class

I am using a dictionary wrapper class, and I want to iterate through it using key-value pairs as seen below 我正在使用字典包装器类,并且想使用键值对进行迭代,如下所示

private void LoadVariables(LogDictionary dic)
{
    foreach (var entry in dic)
    {
        _context.Variables[entry.Key] = entry.Value;
    }

}

but a NotImplementedException is thrown because I did not implement the GetEnumerator() method. 但是NotImplementedExceptionNotImplementedException ,因为我没有实现GetEnumerator()方法。

Here is my wrapper class: 这是我的包装器类:

public class LogDictionary: IDictionary<String, object>
{
    DynamicTableEntity _dte;
    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = dte;
    }
        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        throw new NotImplementedException();
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        throw new NotImplementedException();
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

Assuming you have no special logic that your wrapper needs during enumeration you just forward the calls on to the contained instance: 假设您在枚举过程中没有包装程序需要的特殊逻辑,则只需将调用转发到所包含的实例即可:

public class LogDictionary: IDictionary<String, object>
{
    DynamicTableEntity _dte;
    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = dte;
    }
        bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        throw new NotImplementedException();
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        return _dte.GetEnumerator();
    }

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

You're going to need to implement a List or Dictionary internally to hold the values of your LogDictionary . 您将需要在内部实现一个List或Dictionary来保存LogDictionary的值。

Without knowing what DynamicTableEntity is I will assume it implements IDictionary<string,object> . 在不知道什么是DynamicTableEntity情况下,我将假定它实现了IDictionary<string,object>

public class LogDictionary: IDictionary<String, object>
{
    private IDictionary<String, object> _dte;

    public LogDictionary(DynamicTableEntity dte)
    {
        _dte = (IDictionary<String, object>)dte;
    }

    bool ICollection<KeyValuePair<string, object>>.Remove(KeyValuePair<string, object> item)
    {
        return _dte.Remove(item.Key);
    }

    IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
    {
        return _dte.GetEnumerator();
    }

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

也许您应该派生自Dictionary(而不是IDictionary)并调用base,而不是在方法中引发异常。

暂无
暂无

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

相关问题 实现IEnumerable <T> 和IEnumerable.GetEnumerator()无法公开,为什么? - implementing IEnumerable<T> and IEnumerable.GetEnumerator() can not be public, why? 动态生成的类,实现IEnumerator <T> GetEnumerator()和IEnumerator IEnumerable.GetEnumerator() - Dynamically generated class that implements IEnumerator<T> GetEnumerator() and IEnumerator IEnumerable.GetEnumerator() 有关IEnumerable.GetEnumerator()的错误消息 - Error message regarding IEnumerable.GetEnumerator() 如果 GetEnumerator() 的返回类型不同,IEnumerable.GetEnumerator() 的首选实现如何返回 GetEnumerator()? - How can IEnumerable.GetEnumerator()'s go-to implemetation is to return GetEnumerator(), if GetEnumerator() is of diffrent return type? C#:你如何测试IEnumerable.GetEnumerator()方法? - C#: How do you test the IEnumerable.GetEnumerator() method? 何时调用IEnumerable.GetEnumerator而不是IEnumerable <T> .GetEnumerator? - When does IEnumerable.GetEnumerator get called instead of IEnumerable<T>.GetEnumerator? 实现 IEnumerable,但避免 &#39;MyClass&#39; 没有实现接口成员 &#39;IEnumerable.GetEnumerator()&#39; - Implement IEnumerable, but avoid 'MyClass' does not implement interface member 'IEnumerable.GetEnumerator()' IEnumerable的替代实现GetEnumerator() <T> 包装纸 - Alternative Implementation of GetEnumerator() for IEnumerable<T> Wrapper IEnumerable - 从局部变量使用GetEnumerator实现 - IEnumerable - implementing using GetEnumerator from local variable 如何为实现IEnumerable的类实现GetEnumerator方法 <IEnumerable<T> &gt; - How to Implement GetEnumerator method for class that implements IEnumerable<IEnumerable<T>>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM