C# 如何实现GetEnumerator()和Dispose()?
我为C# 如何实现GetEnumerator()和Dispose()?,c#,C#,我为IAsyncCursor public sealed class DeferredResultCollection<TResult> : IEnumerable<TResult>, IDisposable { private readonly IAsyncCursor<TResult> _asyncCursor; public DeferredResultCollection(IAsyncCursor<TResult> asy
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();
}
}
}
公共密封类DelferredResultCollection:IEnumerable,IDisposable
{
专用只读IAsyncursor\u asyncCursor;
公共延迟结果集合(IAsyncCursor asyncCursor)
{
_asyncCursor=asyncCursor;
}
公共IEnumerator GetEnumerator()
{
对于(;_asyncCursor.MoveNextAsync().Result;)
{
foreach(变量结果为_asyncCursor.Current)
{
收益结果;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共空间处置()
{
如果(_asyncCursor!=null)
{
_asyncCursor.Dispose();
}
}
}
我是C语言的新手,我想添加新的构造函数
IList
,并为IList
实现新方法GetEnumerator()
和Dispose()
。如何做到这一点?请帮帮我。因此,如果我理解正确,您希望从iasyncursor
或IList
中提取值
这很简单:有两个字段,并使用一个不是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();
}
}
}
公共密封类DelferredResultCollection:IEnumerable,IDisposable
{
专用只读IAsyncursor\u asyncCursor;
私有只读ICollection\u结果;
公共延迟结果集合(IAsyncCursor asyncCursor)
{
if(asyncCursor==null)
抛出新ArgumentNullException(nameof(asyncCursor));
_asyncCursor=asyncCursor;
}
公共延迟结果收集(ICollection results)
{
如果(结果==null)
抛出新ArgumentNullException(nameof(results));
_结果=结果;
}
公共IEnumerator GetEnumerator()
{
返回_结果!=null
?_results.GetEnumerator()
:GetAsyncCursorEnumerator();
}
私有IEnumerator GetAsyncCursorEnumerator()
{
对于(;_asyncCursor.MoveNextAsync().Result;)
{
foreach(变量结果为_asyncCursor.Current)
{
收益结果;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
公共空间处置()
{
如果(_asyncCursor!=null)
{
_asyncCursor.Dispose();
}
}
}
如果我正确理解您的问题,我将首先添加第二个私有字段:
private readonly IAsyncCursor<TResult> _asyncCursor;
private readonly IList<TResult> _list;
如果这样做行得通,那么您可以重构代码,看看哪些(如果有的话)可以合并或移动到不同的方法中
请注意,您不能Dispose
一个IList
,因为它不扩展IDisposable
,所以Dispose
方法中没有任何更改
public DeferredResultCollection(IList<TResult> list)
{
_list = list;
}
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;
}
}
}