浏览代码

Let's handle non-nullable as well

Piotr Czajkowski 1 周之前
父节点
当前提交
d74fc78bec

+ 9 - 2
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -10,12 +10,19 @@ public static class TypeExtensions
     {
     {
         if (property == null) return value.GetText();
         if (property == null) return value.GetText();
         
         
-        if (property.PropertyType == typeof(Guid?))
+        if (property.PropertyType == typeof(Guid) || property.PropertyType == typeof(Guid?))
         {
         {
             if (Guid.TryParse(value.GetText(), out var guid))
             if (Guid.TryParse(value.GetText(), out var guid))
                 return guid;
                 return guid;
 
 
-            return null;
+            if (property.PropertyType == typeof(Guid?)) return null;
+            return Guid.Empty;
+        }
+
+        if (property.PropertyType.IsEnum)
+        {
+            return Enum.TryParse(property.PropertyType, value.GetText(), true, out var enumValue)
+                ? enumValue : Enum.GetValues(property.PropertyType).GetValue(0);
         }
         }
 
 
         var nullableUnderlyingType = Nullable.GetUnderlyingType(property.PropertyType);
         var nullableUnderlyingType = Nullable.GetUnderlyingType(property.PropertyType);

+ 2 - 2
ExcelORM/ExcelORMTests/TestAdditionalTypes.cs

@@ -2,6 +2,6 @@ namespace ExcelORMTests;
 
 
 public record TestAdditionalTypes
 public record TestAdditionalTypes
 {
 {
-    public TestEnum? MyEnum {get; set;}
-    public Guid? MyGuid {get; set;}
+    public TestEnum MyEnum {get; set;}
+    public Guid MyGuid {get; set;}
 }
 }

二进制
ExcelORM/ExcelORMTests/testFiles/additionalTypes.xlsx


+ 2 - 0
README.md

@@ -14,6 +14,8 @@ It currently supports properties of types as supported by ClosedXML, so:
 - string
 - string
 - DateTime
 - DateTime
 - TimeSpan
 - TimeSpan
+- Guid
+- enum
 
 
 And their nullable variants.
 And their nullable variants.
 
 

+ 6 - 5
versions.md

@@ -2,8 +2,9 @@
 
 
 | Version | Description |
 | Version | Description |
 | ----------- | ----------- |
 | ----------- | ----------- |
-| 2.0.0 | I've added ability to read data dynamically without a need to create a special type. Useful when you need to read/write some not so organized data.|
-| 2.2.0 | I've added support for formulas, but as this library is based on ClosedXML it has its limitations, as per [Evaluating Formulas](https://github.com/closedxml/closedxml/wiki/Evaluating-Formulas): *Not all formulas are included and you'll probably get a nasty error if the formula isn't supported or if there's an error in the formula. Please test your formulas before going to production.*|
-| 2.3.0 | I've added support for hyperlinks and improved appending to the existing files.|
-| 2.5.0 | I've added Location to ArgumentException Message in ExcelReader. It'll show address of affected cell and the worksheet's name.|
-| 2.6.0 | I've added support for appending starting from given row.|
+| 2.0.0 | Added ability to read data dynamically without a need to create a special type. Useful when you need to read/write some not so organized data.|
+| 2.2.0 | Added support for formulas, but as this library is based on ClosedXML it has its limitations, as per [Evaluating Formulas](https://github.com/closedxml/closedxml/wiki/Evaluating-Formulas): *Not all formulas are included and you'll probably get a nasty error if the formula isn't supported or if there's an error in the formula. Please test your formulas before going to production.*|
+| 2.3.0 | Added support for hyperlinks and improved appending to the existing files.|
+| 2.5.0 | Added Location to ArgumentException Message in ExcelReader. It'll show address of affected cell and the worksheet's name.|
+| 2.6.0 | Added support for appending starting from given row.|
+| 2.7.0 | Added support for reading properties of type Guid and enum.|