Iterator blocks and inheritance

前端 未结 4 1871
無奈伤痛
無奈伤痛 2021-02-13 01:47

Given a base class with the following interface:

public class Base
{
    public virtual IEnumerable GetListOfStuff()
    {
        yield return \"F         


        
4条回答
  •  旧巷少年郎
    2021-02-13 02:23

    It's because the iterator gets turned into a private class, and accessing superclass methods from an inner class is not verifiable (as it has to force the 'this' pointer to something other than itself).

    Try creating a new private method in Derived:

    private IEnumerable GetBaseListOfStuff()
    {        
        return base.GetListOfStuff();
    }
    

    and call that instead of base.GetListOfStuff()

提交回复
热议问题