Ver código fonte

Let's go with base

Piotr Czajkowski 5 meses atrás
pai
commit
93bd87a526

+ 7 - 9
ExcelORM/ExcelORM/ExcelReader.cs

@@ -1,5 +1,6 @@
 using System.Runtime.CompilerServices;
 using ClosedXML.Excel;
+using ExcelORM.Interfaces;
 using ExcelORM.Models;
 
 namespace ExcelORM;
@@ -33,16 +34,13 @@ public class ExcelReader
                 var property = type.GetProperty(item.PropertyName);
                 if (property == null) continue;
 
-                switch (property.PropertyType)
+                switch (property.PropertyType.BaseType)
                 {
-                    case Type formulaType when formulaType == typeof(Formula):
-                        var formula = new Formula
-                        {
-                            FormulaA1 = cell.FormulaA1,
-                            Value = cell.Value.ToObject(),
-                        };
-
-                        property.SetValue(current, formula);
+                    case Type specialProperty when specialProperty == typeof(SpecialBase):
+                        if (RuntimeHelpers.GetUninitializedObject(property.PropertyType) is not SpecialBase special) break;
+                        special.GetValueFromCell(cell);
+
+                        property.SetValue(current, special);
                         break;
                     default:
                         current.SetPropertyValue(property, cell.Value);

+ 1 - 1
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -53,7 +53,7 @@ public class ExcelWriter
                 var valueToSet = property.GetValue(value);
                 if (valueToSet == null) continue;
                 
-                if (valueToSet is ISpecialProperty specialProperty)
+                if (valueToSet is SpecialBase specialProperty)
                 {
                     specialProperty.SetCellValue(worksheet.Cell(rowIndex, cellIndex));
                     continue;

+ 0 - 10
ExcelORM/ExcelORM/Interfaces/ISpecialProperty.cs

@@ -1,10 +0,0 @@
-using ClosedXML.Excel;
-
-namespace ExcelORM.Interfaces
-{
-    internal interface ISpecialProperty
-    {
-        public object? Value { get; set; }
-        public void SetCellValue(IXLCell cell);
-    }
-}

+ 7 - 2
ExcelORM/ExcelORM/Models/Formula.cs

@@ -3,10 +3,15 @@ using ExcelORM.Interfaces;
 
 namespace ExcelORM.Models
 {
-    public class Formula : ISpecialProperty
+    public class Formula : SpecialBase
     {
         public object? Value { get; set; }
         public string? FormulaA1 { get; set; }
-        public void SetCellValue(IXLCell cell) => cell.FormulaA1 = FormulaA1;
+        public override void SetCellValue(IXLCell cell) => cell.FormulaA1 = FormulaA1;
+        public override void GetValueFromCell(IXLCell cell)
+        {
+            Value = cell.Value.ToObject();
+            FormulaA1 = cell.FormulaA1;
+        }
     }
 }

+ 10 - 0
ExcelORM/ExcelORM/Models/SpecialBase.cs

@@ -0,0 +1,10 @@
+using ClosedXML.Excel;
+
+namespace ExcelORM.Models
+{
+    public class SpecialBase
+    {
+        public virtual void SetCellValue(IXLCell cell) { }
+        public virtual void GetValueFromCell(IXLCell cell) { }
+    }
+}