™, , .
, .
class SubjectFactory
{
public IEnumerable<Subject> Read(string filePath)
{
string[] subjectStrings = File.ReadAllLines(filePath);
return Parse(subjectStrings);
}
private IEnumerable<Subject> Parse(IEnumerable<string> subjects)
{
string code = "XYZ";
foreach ( var subject in subjects )
{
yield return new Subject(code, subject);
}
}
}
, Subject , File.ReadAllLines. ? - .
, ?
File.ReadAllLinesAsync(), .
public async Task<IEnumerable<Subject>> ReadAsync(string filePath)
{
string[] subjectStrings = await File.ReadAllLinesAsync(filePath);
return this.Parse(subjectStrings);
}
, , . , .
private async Task<string[]> ReadAllLinesAsync(string filePath)
{
ArrayList allLines = new ArrayList();
using ( var streamReader = new StreamReader(File.OpenRead(filePath)) )
{
string line = await streamReader.ReadLineAsync();
allLines.Add(line);
}
return (string[]) allLines.ToArray(typeof(string));
}
, , ReadAllLinesAsync().
public async Task<IEnumerable<Subject>> ReadAsync(string filePath)
{
string[] subjectStrings = await this.ReadAllLinesAsync(filePath);
return Parse(subjectStrings);
}
When using all of this in your WPF application, all you need to do is the following:
var filePath = @"X:\subjects\";
var subjectFactory = new SubjectFactory();
var subjectsCollection = await subjectFactory.ReadAsync(filePath);
var observableCollection = new ObservableCollection<Subject>(subjectsCollection);
source
share