Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IEnumerable<;T>;-如何对两个枚举数使用一个方法_C#_Enumeration - Fatal编程技术网

C# IEnumerable<;T>;-如何对两个枚举数使用一个方法

C# IEnumerable<;T>;-如何对两个枚举数使用一个方法,c#,enumeration,C#,Enumeration,这可能是个愚蠢的问题,但我还没有找到答案 我有一个实现IEnumerable的简单类。它是文件读取器的基类,用于读取我们从银行收到的EFT平面文件 派生类实现您在代码中看到的抽象GetNext方法,并根据它们读取的行的类型返回行派生类型。最初,我让派生读卡器的调用者在循环中调用GetNext,直到EOF返回null。使用枚举器,它们可以调用foreach,并在读取器中循环 但是为什么我必须实现两个枚举器呢?两者的作用完全相同。我不能通过右键单击=>refactor=>Extract方法重构它来调

这可能是个愚蠢的问题,但我还没有找到答案

我有一个实现
IEnumerable
的简单类。它是文件读取器的基类,用于读取我们从银行收到的EFT平面文件

派生类实现您在代码中看到的抽象
GetNext
方法,并根据它们读取的行的类型返回行派生类型。最初,我让派生读卡器的调用者在循环中调用
GetNext
,直到EOF返回null。使用枚举器,它们可以调用foreach,并在读取器中循环

但是为什么我必须实现两个枚举器呢?两者的作用完全相同。我不能通过右键单击=>refactor=>Extract方法重构它来调用相同的方法,因为该方法包含一个
yield
语句。但我肯定可以使用一个助手方法来处理这两个问题吗?这种方法的签名是什么

using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace EasyDebit.BankInterface
{
    public abstract class FileReader : IEnumerable<KeyValuePair<int, Line>>
    {
        protected int current;
        protected List<string> lines = new List<string>();
        private string filename;

        public FileReader(string filename)
        {
            this.filename = filename;
            this.lines = File.ReadAllLines(filename).ToList();
        }

        public string Filename
        {
            get { return filename; }
        }

        public IEnumerator<KeyValuePair<int, Line>> GetEnumerator()
        {
            Line line = null;
            current = 0;

            while ((line = GetNext()) != null)
                yield return new KeyValuePair<int, Line>(current, line);
        }

        public abstract Line GetNext();

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            Line line = null;
            current = 0;

            while ((line = GetNext()) != null)
                yield return new KeyValuePair<int, Line>(current, line);
        }
    }
}
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
命名空间EasyDebit.BankInterface
{
公共抽象类FileReader:IEnumerable
{
保护电流;
受保护的列表行=新列表();
私有字符串文件名;
公共文件读取器(字符串文件名)
{
this.filename=文件名;
this.lines=File.ReadAllLines(文件名).ToList();
}
公共字符串文件名
{
获取{返回文件名;}
}
公共IEnumerator GetEnumerator()
{
Line=null;
电流=0;
而((line=GetNext())!=null)
返回新的KeyValuePair(当前,行);
}
公共抽象行GetNext();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
Line=null;
电流=0;
而((line=GetNext())!=null)
返回新的KeyValuePair(当前,行);
}
}
}

只需将其强制转换以消除重复代码

    public IEnumerator<KeyValuePair<int, Line>> GetEnumerator()
    {
        Line line = null;
        current = 0;

        while ((line = GetNext()) != null)
            yield return new KeyValuePair<int, Line>(current, line);
    }

    public abstract Line GetNext();

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        return (IEnumerator)GetEnumerator();
    }
public IEnumerator GetEnumerator()
{
Line=null;
电流=0;
而((line=GetNext())!=null)
返回新的KeyValuePair(当前,行);
}
公共抽象行GetNext();
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
返回(IEnumerator)GetEnumerator();
}

不需要演员阵容。非常感谢。它很好用。(没有如上所述的类型转换)