Skip to content

Commit fec56fb

Browse files
committed
Performance improvements. Moved tests to NET 8
1 parent 4e07473 commit fec56fb

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

.github/workflows/dotnet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Setup .NET
2020
uses: actions/setup-dotnet@v3
2121
with:
22-
dotnet-version: 6.0.x
22+
dotnet-version: 8.0.x
2323
- name: Restore dependencies
2424
run: dotnet restore
2525
- name: Build

CsvExport/CsvExport.cs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class CsvExport
4949
/// <summary>
5050
/// The current row
5151
/// </summary>
52-
List<string> _currentRow { get { return _rows[_rows.Count - 1]; } }
52+
List<string> _currentRow = null;
5353

5454
/// <summary>
5555
/// The string used to separate columns in the output
@@ -98,7 +98,10 @@ public CsvExport(string columnSeparator = ",", bool includeColumnSeparatorDefini
9898
/// </summary>
9999
public object this[string field]
100100
{
101-
set {
101+
set
102+
{
103+
if (_currentRow is null) return; // no row has been added yet
104+
102105
// Keep track of the field names
103106
if (!_fields.TryGetValue(field, out int num)) //get the field's index
104107
{
@@ -119,7 +122,8 @@ public object this[string field]
119122
/// </summary>
120123
public void AddRow()
121124
{
122-
_rows.Add(new(_fields.Count));
125+
_currentRow = new(_fields.Count);
126+
_rows.Add(_currentRow);
123127
}
124128

125129
/// <summary>
@@ -129,7 +133,7 @@ public void AddRows<T>(IEnumerable<T> list)
129133
{
130134
if (list.Any())
131135
{
132-
var values = typeof(T).GetProperties();
136+
var values = ReflectionCache<T>.Properties;
133137
foreach (T obj in list)
134138
{
135139
AddRow();
@@ -157,27 +161,24 @@ public static string MakeValueCsvFriendly(object value, string columnSeparator =
157161
if (value == null) return "";
158162
if (value is INullable && ((INullable)value).IsNull) return "";
159163

160-
string output;
161164
if (value is DateTime date)
162165
{
163166
if (date.TimeOfDay.TotalSeconds == 0)
164167
{
165-
output = date.ToString("yyyy-MM-dd");
168+
return date.ToString("yyyy-MM-dd");
166169
}
167170
else
168171
{
169-
output = date.ToString("yyyy-MM-dd HH:mm:ss");
172+
return date.ToString("yyyy-MM-dd HH:mm:ss");
170173
}
171174
}
172-
else
173-
{
174-
output = value.ToString().Trim();
175-
}
175+
176+
var output = value.ToString().Trim();
176177

177178
if (output.Length > 30000) //cropping value for stupid Excel
178179
output = output.Substring(0, 30000);
179180

180-
if (output.Contains(columnSeparator) || output.Contains("\"") || output.Contains("\n") || output.Contains("\r"))
181+
if (output.Contains(columnSeparator) || output.Contains('\"') || output.Contains('\n') || output.Contains('\r'))
181182
output = '"' + output.Replace("\"", "\"\"") + '"';
182183

183184
return output;
@@ -277,4 +278,10 @@ public MemoryStream ExportAsMemoryStream(Encoding encoding = null)
277278
return ms;
278279
}
279280
}
281+
282+
internal static class ReflectionCache<T>
283+
{
284+
private static System.Reflection.PropertyInfo[] _properties;
285+
public static System.Reflection.PropertyInfo[] Properties => _properties ??= typeof(T).GetProperties();
286+
}
280287
}

SpeedBenchmarks/SpeedBenchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
5+
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>

UnitTests/UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

0 commit comments

Comments
 (0)