Replies: 2 comments
-
Hi, did you find an answer to your issue ? Else, I recommand you to add getters and setters to your properties:
Or use fluent ClassMap and remove Log class annotations but keep the getters and setters. |
Beta Was this translation helpful? Give feedback.
-
This is configurable for CsvHelper, so if you need to keep them fields configure the writer to map fields, see these comments on an issue from 2018. But that said, I agree with @remymantei. The convention for most .NET/C# code is: properties are serialized, while fields are not, even public fields. If possible it would be better to switch your code to follow the platform conventions. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to write into a csv file by using Attributes.
I maybe should mension, that i am working in Unity.
This is my code:
public class Log
{
[Index(0)]
[Name("Average Time")]
public float timeAverage;
[Index(2)]
[Name("Tries")]
public float numberOfTries;
[Index(1)]
[Name("TPT")]
public flaot averageTimePerTry;
}
public void MakeFile(Log log)
{
using (var writer = new StreamWriter(Application.dataPath + "/Logs/logFile.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecord<Log>(log);
}
}
So this gives me the error "No properties are mapped for type..."
I also tried to use the ClassMap method and it worked, but i didn't have any headers. The code looked like this:
public class LogFileMap : ClassMap<Log>
{
public LogFileMap()
{
Map(m => m.timeAverage).Index(0).Name("Average Time");
Map(m => m.numberOfTries).Index(1).Name("Number of tries");
Map(m => m.averageTimePerTry).Index(2).Name("Time on one Try");
}
}
with the addidtion of
csv.Configuration.RegisterClassMap<ClassMap>();
in theMakeLog()
function.If possible, i would like to know how to do this with the Attributes. But i will also be thankful for a fix in the ClassMap method.
Best regards.
Beta Was this translation helpful? Give feedback.
All reactions