我有以下问题:
我想实现自己的集合,它也将实现ICollection<T>
接口。这意味着我还需要实现IEnumerable<T>
接口。实施IEnumerable<T>
可以通过两种方式实现:
第一种方法:让私有结构实现IEnumerator<T>
并从GetEnumerator()
方法返回
第二种方法:我可以使用iterator(使用yield return
)并让编译器为我生成IEnumerator
类。
我还希望我的调查员在修改集合后在任何InvalidOperationException
(或MoveNext()
)调用上抛出Reset()
as it is specified in MS documentation
如果我将使用第一种方法,那么一切正常(我只是在创建新的枚举器时保存我的集合的版本,然后在MoveNext()
我只是检查它是否没有改变)。
MS Collections使用相同的技巧但是这里是问题 - 如果我将使用第二种方法我认为我不能强制执行以下行为。请考虑以下代码。
class MyCollection : ICollcetion<T>
{
int _version = 0;
T[] _collectionData;
public void Modify()
{
... // do the modification
_version++; // change version, so enumerators can check that they are invalid
}
...
public IEnumerator<T> GetEnumerator()
{
int _enmVersion = _version;
int i = 0;
yield return _collectionData[i];
if( _enmVersion != _version ) // check if there was not modificaction
{
throw new InvalidOperationException();
}
}
...
}
...
var collection = new MyCollection();
var e = collection.GetEnumerator();
myCollection.Modfiy(); //collection modification, so e becomes irrecoverably invalidated
e.MoveNext(); // right now I want to throw exception
//but instead of that, code until first yield return executed
//and the version is saved... :(
只有在收集枚举器对象后修改了集合,但在第一次调用MoveNext()
之前,才会出现此问题,但仍然存在问题......
我的问题是:是否有可能以某种方式使用第二种方法并强制执行正确的行为或我需要本案例坚持第一种方法?
我的怀疑是我必须使用第一种方法,因为迭代器方法的代码仅在我调用MoveNext()
时才起作用,而不是在编译器生成的类的构造函数中,但是我我想确定......
答案 0 :(得分:1)
您的问题是,在第一次glGetFramebufferAttachmentParameteriv
调用之前,迭代器方法根本不会运行任何代码。
您可以通过将迭代器包装在非迭代器方法中来解决此问题,该方法立即获取版本并将其作为参数传递:
MoveNext()