在迭代器中访问C#基类会导致ReSharper警告 [英] accessing C# base class in iterator causes ReSharper warning
问题描述
我有两个类, GenericList
和 SpecificList
,其中 SpecificList
继承自 GenericList
。 GenericList
实现 IEnumerable< GenericItem>
和 SpecificList
实现的IEnumerable< SpecificItem>
。 SpecificItem
继承自 GenericItem
。我必须在 GenericList
和 SpecificList
中实现 GetEnumerator
他们实现 IEnumerable< T>
。在 GenericList
中,这很简单,我只返回基础列表< T>
的枚举器:
I have two classes, GenericList
and SpecificList
, where SpecificList
inherits from GenericList
. GenericList
implements IEnumerable<GenericItem>
and SpecificList
implements IEnumerable<SpecificItem>
. SpecificItem
inherits from GenericItem
. I have to implement GetEnumerator
in both GenericList
and SpecificList
since they implement IEnumerable<T>
. In GenericList
, it's easy enough, I just return the enumerator for the underlying List<T>
:
public IEnumerator<GenericItem> GetEnumerator()
{
return genericItemsList.GetEnumerator();
}
但是,在$ code> SpecificList ,看起来比较棘手。将 IEnumerator< GenericItem>
转换为 IEnumerator< SpecificItem>
似乎有风险,我不知道是否会甚至工作。相反,我做了以下事情:
However, in SpecificList
, it seems trickier. It seems risky to cast IEnumerator<GenericItem>
to IEnumerator<SpecificItem>
, and I don't know if that would even work. Instead, I did the following:
public new IEnumerator<SpecificItem> GetEnumerator()
{
IEnumerator<GenericItem> enumerator = base.GetEnumerator();
while (enumerator.MoveNext())
{
yield return (SpecificItem)enumerator.Current;
}
}
这个编译很好,一个简单的MSTest单元测试调用 SpecificList.GetEnumerator()
似乎表明它有效。但是,ReSharper在上述方法中突出显示 base
,并显示以下警告:
This compiles fine and a simple MSTest unit test calling SpecificList.GetEnumerator()
seems to show it works. However, ReSharper highlights base
in the above method with the following warning:
Access从匿名方法,lambda表达式,查询表达式或迭代器通过'base'关键字到GenericList.GetEnumerator导致无法验证的代码
Access to GenericList.GetEnumerator through 'base' keyword from anonymous method, lambda expression, query expression or iterator results in unverifiable code
这是什么东西我应该担心吗?我应该采用不同的方式吗?
Is this something I should worry about? Should I do something differently?
编辑:我正在使用ReSharper 5.1完整版预发布版本5.1.1715.35。
I'm using ReSharper 5.1 Full Edition Pre-Release Build 5.1.1715.35.
另外,我应该暂停运行MSTest单元测试:我只需按 Ctrl + R , Ctrl < Chrome中的/ kbd> + T 重新加载页面...
Also, I should take a break from running MSTest unit tests: I just hit Ctrl+R, Ctrl+T in Chrome to reload the page...
推荐答案
R#是正确的在3.0版本的C#编译器中访问迭代器/ lambda中的 base
关键字可能会导致无法验证的代码。当它有或没有时有点复杂,我不会试图在这里覆盖它。
R# is correct that accessing the base
keyword inside an iterator / lambda in the 3.0 version of the C# compiler could result in unverifiable code. When it does or doesn't is a bit complex and I won't attempt to cover it here.
解决此问题的最简单方法是将调用包装到另一个非静态私有的 base.GetEnumerator
中来自迭代器的方法和引用。
The easiest way to work around this is to wrap the call to base.GetEnumerator
in another non-static private method and reference that from your iterator.
private IEnumerator<GenericItem> GetBaseEnumerator() {
return base.GetEnumerator();
}
public new IEnumerator<SpecificItem> GetEnumerator()
{
IEnumerator<GenericItem> enumerator = GetBaseEnumerator();
while (enumerator.MoveNext())
{
yield return (SpecificItem)enumerator.Current;
}
}
我很确定这个错误已在4.0版本的C#编译器。
I'm fairly certain this bug was fixed in the 4.0 version of the C# compiler.
这篇关于在迭代器中访问C#基类会导致ReSharper警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!