C# IEnumerator实现问题
我有以下代码:C# IEnumerator实现问题,c#,collections,ienumerator,C#,Collections,Ienumerator,我有以下代码: public class Check21CSVRecord { public string TransactionType; public string TransactionCurrency; public string Reference; public string PaymentType; // date of transaction public string TransactionDa
public class Check21CSVRecord
{
public string TransactionType;
public string TransactionCurrency;
public string Reference;
public string PaymentType;
// date of transaction
public string TransactionDate;
public string Notes;
// customer data goes here
public string CustomerFirstName;
public string CustomerInitial;
public string CustomerLastName;
public string CustomerStreetAddress;
public string CustomerCity;
public string CustomerState;
public string CustomerZIPCode;
[FieldConverter(ConverterKind.Date, "MM/dd/yy")]
public DateTime CustomerDateBirth;
public string CustomerCountry;
public string CustomerEmail;
public string CustomerPhone;
public string CustomerIPAddress;
// MICR line goes here
public string AuxiliaryOnUs;
public string ExternalProcessingCode;
public string PayorBankRoutingNumber;
public string PayorBankRoutingNumberCheckDigit;
public string OnUs;
// check amount
[FieldConverter(ConverterKind.Double)]
public double Amount;
// what account to credit
public string CreditAccountNumber;
public string FrontImagePath;
public string BackImagePath;
// used to define if we have image or should build it dynamically
public bool IsDynamicImage{
get
{
return (FrontImagePath.Length == 0 && BackImagePath.Length == 0);
}
}
}
public class DataProvider <T>: IEnumerator
{
private FileHelperEngine CSVReader;
private T currentRecord;
private int currentIndex;
private string file;
private T[] collection = new T[5000];
public DataProvider(string CSVFile)
{
CSVReader = new FileHelperEngine(typeof(T));
collection = CSVReader.ReadFile(file) as T[];
currentRecord = default(T);
file = CSVFile;
Reset();
}
public bool MoveNext()
{
if (++currentIndex >= collection.Count())
{
return false;
}
else
{
currentRecord = collection[currentIndex];
}
return true;
}
public void Reset()
{
currentIndex = -1;
}
public void Dispose()
{
}
public T Current
{
get
{
return currentRecord;
}
}
object System.Collections.IEnumerator.Current
{
get { return currentRecord; }
}
}
公共类Check21CSVRecord
{
公共字符串事务类型;
公共字符串事务货币;
公共字符串引用;
公共字符串PaymentType;
//交易日期
公共字符串TransactionDate;
公共弦乐;
//客户数据在这里
公共字符串CustomerFirstName;
公共字符串CustomerInitial;
公共字符串CustomerLastName;
公共字符串CustomerStreetAddress;
公共字符串客户;
公共字符串CustomerState;
公共字符串CustomerZIPCode;
[FieldConverter(ConverterKind.Date,“MM/dd/yy”)]
公共日期时间客户日期出生;
公共字符串CustomerCountry;
公共字符串CustomerEmail;
公共字符串自定义电话;
公共字符串CustomerIPAddress;
//MICR线在这里
公共字符串辅助;
公共字符串外部处理代码;
公共字符串PayorBankRoutingNumber;
公共字符串支付银行RoutingNumberCheckDigit;
公共责任;
//支票金额
[FieldConverter(ConverterKind.Double)]
公共双倍金额;
//贷记什么账户
公共字符串CreditAccountNumber;
公共字符串路径;
公共字符串返回路径;
//用于定义是否有映像或是否应动态构建映像
公众形象{
得到
{
返回值(FrontImagePath.Length==0&&BackImagePath.Length==0);
}
}
}
公共类数据提供程序:IEnumerator
{
私人文件帮助工程师CSVReader;
私人档案;
私有int-currentIndex;
私有字符串文件;
私有T[]集合=新T[5000];
公共数据提供程序(字符串CSVFile)
{
CSVReader=新文件HelperEngine(类型为(T));
collection=CSVReader.ReadFile(文件)作为T[];
currentRecord=默认值(T);
file=CSVFile;
重置();
}
公共图书馆
{
如果(++currentIndex>=collection.Count())
{
返回false;
}
其他的
{
currentRecord=集合[currentIndex];
}
返回true;
}
公共无效重置()
{
currentIndex=-1;
}
公共空间处置()
{
}
公共电流
{
得到
{
返回电流记录;
}
}
对象System.Collections.IEnumerator.Current
{
获取{return currentRecord;}
}
}
当我编译时-一切都很好,但是当我调用类时:
var input = new DataProvider<Check21CSVRecord>("../../../../Artifacts/IncomingData/Check21/check21.csv");
while (input.MoveNext())
{
Console.WriteLine(input.Current.CustomerFirstName);
}
var input=newdataprovider(“../../../../Artifacts/IncomingData/Check21/Check21.csv”);
while(input.MoveNext())
{
Console.WriteLine(input.Current.CustomerFirstName);
}
我遇到了以下问题:
有没有办法解决这个问题
谢谢,
Dmitry您应该首先分配
文件
public DataProvider(string CSVFile)
{
file = CSVFile;
CSVReader = new FileHelperEngine(typeof(T));
collection = CSVReader.ReadFile(file) as T[];
currentRecord = default(T);
Reset();
}
是的,因为“文件”
”是空的/空的