Procházet zdrojové kódy

Added ToObject_DateOnlyAsString

Piotr Czajkowski před 1 týdnem
rodič
revize
a22f65a4dc

+ 3 - 2
ExcelORM/ExcelORM/ExcelORM.csproj

@@ -4,7 +4,7 @@
         <TargetFramework>net8.0</TargetFramework>
         <ImplicitUsings>enable</ImplicitUsings>
         <Nullable>enable</Nullable>
-        <Version>2.8.1</Version>
+        <Version>2.9.0</Version>
         <PackageProjectUrl>https://git.liox.eu/pczajkowski/ExcelORM</PackageProjectUrl>
         <RepositoryUrl>https://github.com/pczajkowski/ExcelORM</RepositoryUrl>
         <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
@@ -14,7 +14,8 @@
 	<Authors>Piotr Czajkowski</Authors>
 	<Description>Simple library to read/write C# objects from/to Excel files. </Description>
 	<RepositoryType>GitHub</RepositoryType>
-	<PackageReleaseNotes>Ability to start writing from given row.</PackageReleaseNotes>
+	<PackageReleaseNotes>Ability to start writing from given row.
+Trying to handle dates saved as text without throwing.</PackageReleaseNotes>
     </PropertyGroup>
 
     <ItemGroup>

+ 3 - 0
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -38,6 +38,9 @@ public static class TypeExtensions
         if (property.PropertyType == typeof(DateTime) || property.PropertyType == typeof(DateTime?))
             return DateTime.TryParse(value.GetText(), out var dateTime) ? dateTime : default;
 
+        if (property.PropertyType == typeof(DateOnly) || property.PropertyType == typeof(DateOnly?))
+            return DateOnly.TryParse(value.GetText(), out var dateOnly) ? dateOnly : default;
+
         var nullableUnderlyingType = Nullable.GetUnderlyingType(property.PropertyType);
         if (property.PropertyType.IsEnum || (nullableUnderlyingType is { IsEnum: true }))
             return HandleEnum(value, property, nullableUnderlyingType);

+ 12 - 0
ExcelORM/ExcelORMTests/TypeExtensionsTests.cs

@@ -16,4 +16,16 @@ public class TypeExtensionsTests
         var readValue = value.ToObject(propertyInfo);
         Assert.IsType<DateTime>(readValue);
     }
+    
+    public DateOnly? DateOnlyProperty { get; set; }
+    
+    [Fact]
+    public void ToObject_DateOnlyAsString()
+    {
+        XLCellValue value = "7/27/2025";
+        
+        var propertyInfo = typeof(TypeExtensionsTests).GetProperty("DateOnlyProperty");
+        var readValue = value.ToObject(propertyInfo);
+        Assert.IsType<DateOnly>(readValue);
+    }
 }