Piotr Czajkowski пре 6 месеци
родитељ
комит
ebf1aed364
2 измењених фајлова са 25 додато и 12 уклоњено
  1. 2 12
      ExcelORM/ExcelORM/ExcelReader.cs
  2. 23 0
      ExcelORM/ExcelORM/TypeExtensions.cs

+ 2 - 12
ExcelORM/ExcelORM/ExcelReader.cs

@@ -1,3 +1,4 @@
+using System.Reflection;
 using ClosedXML.Excel;
 
 namespace ExcelORM;
@@ -31,18 +32,7 @@ public class ExcelReader
                 var property = current.GetType().GetProperty(item.PropertyName);
                 if (property == null) continue;
 
-                object? valueToSet = property.PropertyType switch
-                {
-                    not null when property.PropertyType == typeof(string) => cell.Value.ToString(),
-                    not null when property.PropertyType == typeof(DateTime?) => cell.Value.IsDateTime ? cell.Value.GetDateTime() : null,
-                    not null when property.PropertyType == typeof(TimeSpan?) => cell.Value.IsTimeSpan ? cell.Value.GetTimeSpan() : null,
-                    not null when property.PropertyType == typeof(double?) => cell.Value.IsNumber ? cell.Value.GetNumber() : null,
-                    not null when property.PropertyType == typeof(int?) => cell.Value.IsNumber ? (int?)cell.Value.GetNumber() : null,
-                    _ => throw new NotSupportedException($"{property.PropertyType} isn't supported!")
-                };
-               
-                if (valueToSet != null)
-                    property.SetValue(current, valueToSet);
+                current.SetValue(property, cell);
             }
 
             yield return current;

+ 23 - 0
ExcelORM/ExcelORM/TypeExtensions.cs

@@ -0,0 +1,23 @@
+using System.Reflection;
+using ClosedXML.Excel;
+
+namespace ExcelORM;
+
+public static class TypeExtensions
+{
+    public static void SetValue<T>(this T currentObject, PropertyInfo property, IXLCell cell)
+    {
+        object? valueToSet = property.PropertyType switch
+        {
+            not null when property.PropertyType == typeof(string) => cell.Value.ToString(),
+            not null when property.PropertyType == typeof(DateTime?) => cell.Value.IsDateTime ? cell.Value.GetDateTime() : null,
+            not null when property.PropertyType == typeof(TimeSpan?) => cell.Value.IsTimeSpan ? cell.Value.GetTimeSpan() : null,
+            not null when property.PropertyType == typeof(double?) => cell.Value.IsNumber ? cell.Value.GetNumber() : null,
+            not null when property.PropertyType == typeof(int?) => cell.Value.IsNumber ? (int?)cell.Value.GetNumber() : null,
+            _ => throw new NotSupportedException($"{property.PropertyType} isn't supported!")
+        };
+               
+        if (valueToSet != null)
+            property.SetValue(currentObject, valueToSet); 
+    }
+}