Override IEnumerable Where

后端 未结 2 1823
小鲜肉
小鲜肉 2021-01-24 03:24

I\'ve written a class that implements IEnumerable :

public class MyEnumerable : IEnumerable
{ 
    IEnumerator IEnumerable.GetEnumerator()
    {
         


        
相关标签:
2条回答
  • 2021-01-24 03:55

    Sure - just add a Where method to MyEnumerable. The Linq Where method is an extension method, so it's not technically an override. you're "hiding" the linq method.

    public class MyEnumerable : IEnumerable<MyClass>
    { 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        public IEnumerator<MyClass> GetEnumerator()
        {
            //Enumerate
        }
    
        public MyEnumerable Where()
        {
           // implement `Where`
        }
    }
    

    There are some caveats, though:

    • Your Where method will only be called if the declared type is MyEnumerable - it will not be called on variables of type IEnumerable<MyClass> (or any collection that implements it, like List<MyClass>
    • There are several overloads of Where that will need to be implemented as well if you want to maintain consistently with Linq.
    0 讨论(0)
  • 2021-01-24 04:13

    Update

    From your comment your enumerator is a lazy file enumerator and you want to be able to select items from it based on a predicate and still have the laziness.

    You could create another class inheriting that class or an interface to help with this.

    Here is an example

    public class FileItem
    {
        //Some properties
    }
    
    public interface IFileEnumerator : IEnumerable<FileItem>
    {
        IFileEnumerator Where(Func<FileItem, bool> predicate);
    }
    
    public class FileEnumerator : IFileEnumerator
    {
        private readonly string fileName;
    
        public FileEnumerator(string fileName)
        {
            this.fileName = fileName;
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    
        public IEnumerator<FileItem> GetEnumerator()
        {
            var items = new List<FileItem>();
    
            //Read from file and add lines to items
    
            return items.GetEnumerator();
        }
    
        public IFileEnumerator Where(Func<FileItem, bool> predicate)
        {
            return new MemoryEnumerator(ToEnumerable(GetEnumerator()).Where(predicate));
        }
    
        private static IEnumerable<T> ToEnumerable<T>(IEnumerator<T> enumerator) 
        {
            while (enumerator.MoveNext()) 
            {
                yield return enumerator.Current;
            }
        }
    }
    
    public class MemoryEnumerator : IFileEnumerator
    {
        private readonly IEnumerable<FileItem> items;
    
        public MemoryEnumerator(IEnumerable<FileItem> items)
        {
            this.items = items;
        }
    
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    
        public IEnumerator<FileItem> GetEnumerator()
        {
            return items.GetEnumerator();
        }
    
        public IFileEnumerator Where(Func<FileItem, bool> predicate)
        {
            return new MemoryEnumerator(items.Where(predicate));
        }
    }
    
    0 讨论(0)
提交回复
热议问题