创建List<Module>
并添加从您的文件中读取的所有项目:
List<Module> modules = new List<Module>();
while (!reader.EndOfStream)
{
lineFromFile = reader.ReadLine();
split = lineFromFile.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var newModule = new Module();
newModule.Property1 = split[0];
newModule.Property2 = split[1];
// (...) //
modules.Add(newModule);
}
或使用 LINQ:
var modules = (from line in File.ReadAllLines("fileName")
let parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
select new Module() {
Property1 = parts[0],
Property2 = parts[1],
Property3 = parts[2],
}).ToList();