I'm using csvHelper for reading txt file where columns can have names like this "column1 ".
Of course I don't want to put such ugly string in my ColumnAttribute
[ColumnInfo(typeof(string), "column1")
public string Column1{get;set;}
That is why, before
var reader = new CsvReader(path);
var result = reader.GetRecords<MyObject>();
I want to pre process column names of the file before read it.
So, I've made preprocessor
public void TrimmColumns(ref StreamReader reader)
{
var columns = reader.ReadLine();
if (columns != null)
{
columns = string.Join(";", columns.Split(';').Select(str => str.Trim()).ToArray());
var res = columns + reader.ReadToEnd();
var stream = new MemoryStream();
var streamWriter = new StreamWriter(stream);
streamWriter.Write(res);
streamWriter.Flush();
stream.Position = 0;
reader = new StreamReader(stream);
}
else
{
throw new ArgumentException("Host file is empty");
}
}
And now I stuck. I can create stream reader from the string and return it but for me it looks not best decision. Can anyone with real experience explain how to make it not only work but also satisfy normal coding style?
[Updated] that is how I'm doing now, but it isn't looks nice at all... [Updated 2] I just realized that it is also bad, coz I'm closing one stream and create other inside this function.