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

C# 在包装器字典类中实现IEnumerable.GetEnumerator(),c#,dictionary,interface,ienumerable,C#,Dictionary,Interface,Ienumerable,我使用的是一个字典包装类,我想使用键值对遍历它,如下所示 private void LoadVariables(LogDictionary dic) { foreach (var entry in dic) { _context.Variables[entry.Key] = entry.Value; } } 但是抛出了一个NotImplementedException,因为我没有实现GetEnumerator()方法 这是我的包装器类: public

我使用的是一个字典包装类,我想使用键值对遍历它,如下所示

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

}
但是抛出了一个
NotImplementedException
,因为我没有实现
GetEnumerator()
方法

这是我的包装器类:

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();
    }
}
公共类日志字典:IDictionary
{
动态实体;
公共日志字典(DynamicTableEntity dte)
{
_dte=dte;
}
布尔ICollection.Remove(KeyValuePair项)
{
抛出新的NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
抛出新的NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
抛出新的NotImplementedException();
}
}

也许您应该从Dictionary(而不是IDictionary)和call base派生,而不是在方法中抛出异常。

假设您在枚举期间没有包装器需要的特殊逻辑,您只需将调用转发到包含的实例:

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();
    }
公共类日志字典:IDictionary
{
动态实体;
公共日志字典(DynamicTableEntity dte)
{
_dte=dte;
}
布尔ICollection.Remove(KeyValuePair项)
{
抛出新的NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回_dte.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}

您需要在内部实现一个列表或字典来保存
日志字典的值

在不知道
DynamicTableEntity
是什么的情况下,我假设它实现了
IDictionary

公共类日志字典:IDictionary
{
私人词典;
公共日志字典(DynamicTableEntity dte)
{
_dte=(IDictionary)dte;
}
布尔ICollection.Remove(KeyValuePair项)
{
返回_dte.Remove(item.Key);
}
IEnumerator IEnumerable.GetEnumerator()
{
返回_dte.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}

它的可能副本不能充当
DynamicTableEntity
的包装器,因此这并不能回答问题。
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();
    }
}