Open DeltaZsnes opened 3 years ago
What version are you on?
You should register your class map like this
csvWriter.Context.RegisterClassMap<T>();
You don't need to call csvWriter.Flush()
. That will happen automatically.
You don't have any braces after your using
statements, so there is no block to dispose of. My guess is dispose is never called, which means the StreamWriter
is never flushed. You'll need to put braces after, or manually call streamWriter.Flush()
to flush the StreamWriter
's buffer to the file.
Hi Josh. Cool to get an immediate answer from the author himself.
The version used is 16.0.0 from NuGetPackage
Flush is not the problem here I just included that code to be extra sure Dispose is called after it was not included in the snippet
From my understanding registering a class map is optional? I have other projects (also visual studio projects) where the same code work just fine without registering a class map. It is a very weird ninja bug.
There isn't enough information in your example for me to figure out anything else.
Can you make a small example that shows the error?
Maybe you can modify this example.
void Main()
{
var records = new List<Foo>
{
new Foo { Id = 1, Name = "one" },
};
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
};
using (var writer = new StringWriter())
using (var csv = new CsvWriter(writer, config))
{
csv.WriteRecords(records);
writer.Dump();
}
}
private class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
private class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.Id);
Map(m => m.Name);
}
}
note: Dump is from LINQPad
What version are you on?
You should register your class map like this
csvWriter.Context.RegisterClassMap<T>();
You don't need to call
csvWriter.Flush()
. That will happen automatically.You don't have any braces after your
using
statements, so there is no block to dispose of. My guess is dispose is never called, which means theStreamWriter
is never flushed. You'll need to put braces after, or manually callstreamWriter.Flush()
to flush theStreamWriter
's buffer to the file.
I was thought the streamWriter will flush when I called csvWriter.Flush()
,is it not?
CsvWriter
works just like StreamWriter
. It has an internal buffer that will flush to the TextWriter
on it's own and when disposed. If you don't dispose after writing, you'll need to manually call CsvWriter.Flush()
to flush the data in the buffer to the TextWriter
and flush the TextWriter
itself.
Describe the bug When using the CsvWriter in C# Visual Studion Pro 2019. I would sometimes not be able to write the CSV Header to the file. The file would finish as a blank file. After looking through your source code for a few hours I figured out a very simple fix. The code is included in the bottom. Apparently the class map is not always registered in the context so the fix makes sure to always register it. Thanks for an otherwise great library.
When trying to only write the header the file is empty When trying to write the records an exception thrown is "No properties are mapped for type"
To Reproduce Difficult to reproduce seems to work well in some projects and not at all in other projects. The fix seems to work for all projects.
Expected behavior When writing a csv file with headers included it should just work
Screenshots N/A
Additional context N/A
Code