如何实现GetEnumerator()和Dispose()?

时间:2015-11-18 18:14:04

标签: c#

我有IAsyncCursor

的以下包装类
public sealed class DeferredResultCollection<TResult> : IEnumerable<TResult>, IDisposable
{
    private readonly IAsyncCursor<TResult> _asyncCursor;

    public DeferredResultCollection(IAsyncCursor<TResult> asyncCursor)
    {
        _asyncCursor = asyncCursor;
    }

    public IEnumerator<TResult> GetEnumerator()
    {
        for (; _asyncCursor.MoveNextAsync().Result;)
        {
            foreach (var result in _asyncCursor.Current)
            {
                yield return result;
            }
        }
    }

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

    public void Dispose()
    {
        if (_asyncCursor != null)
        {
            _asyncCursor.Dispose();
        }
    }
}

我是C#的新手,我想添加新的构造函数IList<TResult>,并为GetEnumerator()实现新方法Dispose()IList<TResult>。这该怎么做?求你帮帮我。

2 个答案:

答案 0 :(得分:1)

因此,如果我理解正确,您可以从IAsyncCursor<T>IList<T>中提取值。

这很简单:有两个字段,并使用不是null的字段。

public sealed class DeferredResultCollection<TResult> : IEnumerable<TResult>, IDisposable
{
    private readonly IAsyncCursor<TResult> _asyncCursor;
    private readonly ICollection<TResult> _results;

    public DeferredResultCollection(IAsyncCursor<TResult> asyncCursor)
    {
        if (asyncCursor == null)
            throw new ArgumentNullException(nameof(asyncCursor));

        _asyncCursor = asyncCursor;
    }

    public DeferredResultCollection(ICollection<TResult> results)
    {
        if (results == null)
            throw new ArgumentNullException(nameof(results));

        _results = results;
    }

    public IEnumerator<TResult> GetEnumerator()
    {
        return _results != null
            ? _results.GetEnumerator()
            : GetAsyncCursorEnumerator();
    }

    private IEnumerator<TResult> GetAsyncCursorEnumerator()
    {
        for (; _asyncCursor.MoveNextAsync().Result;)
        {
            foreach (var result in _asyncCursor.Current)
            {
                yield return result;
            }
        }
    }

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

    public void Dispose()
    {
        if (_asyncCursor != null)
        {
            _asyncCursor.Dispose();
        }
    }
}

答案 1 :(得分:1)

如果我理解你的问题,我会先添加第二个私人字段:

 private readonly IAsyncCursor<TResult> _asyncCursor;
 private readonly IList<TResult> _list;

和构造函数:

public DeferredResultCollection(IList<TResult> list)
{
    _list = list;
}

并且只检查GetEnumerator中填充了哪一个:

public IEnumerator<TResult> GetEnumerator()
{
    if(_list) != null
        foreach(var item in _list)
        {
            yield return item;
        }
    if(_asyncCursor) != null
        for (; _asyncCursor.MoveNextAsync().Result;)
        {
            foreach (var result in _asyncCursor.Current)
            {
                yield return result;
            }
        }
    }

如果可行,那么您可以查看重构代码,看看可以将哪些(如果有的话)组合或移动到单独的方法中。

请注意,您不能Dispose IList,因为它不会延伸IDisposable,因此Dispose方法无需更改。