【发布时间】:2018-03-26 17:46:18
【问题描述】:
我想在运行时将对象类型传递给IEnumerable<T>。
例如,我必须读取 CSV 文件,它们都返回不同的数据集。
所以如果我想使用单一方法但提取一组不同的实体,我该怎么做?在下面显示的示例中,我想传递不同的实体而不是 Student:
public List<Patient> GetAcgFileData(string fullFileName)
{
using (var sr = new StreamReader(fullFileName))
{
var reader = new CsvReader(sr);
////CSVReader will now read the whole file into an enumerable
var records = reader.GetRecords<Student>().ToList();
return records;
}
}
【问题讨论】:
-
为什么不让自己的方法通用?
标签: c# ienumerable