This package allows for generating static C# classes allowing for easy, type safe and performant row-wise access to the data.
Add a package reference to your project:
<ItemGroup>
<PackageReference Include="CsvSourceGeneration" Version="0.0.2" />
</ItemGroup>Then include a file with the .csv extension:
<ItemGroup>
<AdditionalFiles Include="CsvFiles\MyFile.csv" />
</ItemGroup>Entry,Message
First,Hello World!
When the project is built next, the data contained in the csv is available through a static class with the same name as the csv file:
Console.WriteLine($"Some data from the csv file: {MyFile.First.Message}");
// Some data from the csv file: Hello World!Several field types (FieldType enumerable) are supported.
The data type of a column is deduced from the first row based on the following rules:
- If the column name is
id(ignoring case), values are parsed intoIdString - If the cell value ends with
s, values are parsed intoTimeSpanusingTimeSpan.FromSeconds - If the cell value can be parsed with
bool.TryParse, values are parsed intobool - If the cell value can be parsed with
int.TryParse, values are parsed intoint - If the cell value can be parsed with
float.TryParse, values are parsed intofloat - If the cell value can be parsed with
double.TryParse, values are parsed intodouble - Otherwise, values are assumed to be strings.
Example CSV content with each field type:
| Example | Id | TimeSpan | Bool | Int | Float | Double | String |
|---|---|---|---|---|---|---|---|
| First | first | 1.0s | true | 10 | 10.0 | 10.0d | Hello World! |