【发布时间】:2018-02-22 05:34:26
【问题描述】:
我有一种情况,我有我要派生的迭代器类。基迭代器迭代基对象,派生迭代器迭代派生对象。我可以在基迭代器上使用 LINQ,但不能在派生迭代器上使用。如果我可以使用 LINQ 遍历其中任何一个,那将非常有帮助。
提前感谢您的指导!
namespace ClassLibrary1
{
public class BaseThingy { }
public class DerivedThingy : BaseThingy { }
public class BaseEnumerator : IReadOnlyCollection<BaseThingy>
{
public int Count => throw new NotImplementedException();
public IEnumerator<BaseThingy> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class DerivedEnumerator : BaseEnumerator, IReadOnlyCollection<DerivedThingy>
{
IEnumerator<DerivedThingy> IEnumerable<DerivedThingy>.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class Application
{
public void Main()
{
BaseEnumerator baseEnumerator = new BaseEnumerator();
DerivedEnumerator derivedEnumerator = new DerivedEnumerator();
// Works...
var x = baseEnumerator.Where(s => s.GetHashCode() == s.GetHashCode());
// Doesn't work...
var y = derivedEnumerator.Where(s => s.GetHashCode() == s.GetHashCode());
}
}
}
【问题讨论】:
-
“不起作用...” 以哪种方式?能不能给出编译错误?
标签: c# linq inheritance ienumerable