Bläddra i källkod

No need for new constraint

Piotr Czajkowski 6 månader sedan
förälder
incheckning
c11d96c27d
3 ändrade filer med 13 tillägg och 10 borttagningar
  1. 9 6
      ExcelORM/ExcelORM/ExcelReader.cs
  2. 3 3
      ExcelORM/ExcelORM/ExcelWriter.cs
  3. 1 1
      ExcelORM/ExcelORM/Mapping.cs

+ 9 - 6
ExcelORM/ExcelORM/ExcelReader.cs

@@ -1,3 +1,4 @@
+using System.Runtime.Serialization;
 using ClosedXML.Excel;
 using ExcelORM.Models;
 
@@ -14,14 +15,16 @@ public class ExcelReader
         xlWorkbook = new XLWorkbook(path);
     }
 
-    private IEnumerable<T> ProcessRows<T>(IEnumerable<IXLRow> rows, List<Mapping> mapping) where T : class, new()
+    private IEnumerable<T> ProcessRows<T>(IEnumerable<IXLRow> rows, List<Mapping> mapping) where T : class
     {
         var type = typeof(T);
         foreach (var row in rows)
         {
             if (SkipHidden && row.IsHidden) continue;
 
-            var current = new T();
+            var current = FormatterServices.GetUninitializedObject(typeof(T)) as T;
+            if (current == null) continue;
+
             foreach (var item in mapping)
             {
                 if (item.Position == null || item.PropertyName == null) continue;
@@ -53,7 +56,7 @@ public class ExcelReader
         }
     }
 
-    private IEnumerable<T> Read<T>(IXLWorksheet? worksheet, uint startFrom, uint skip) where T : class, new()
+    private IEnumerable<T> Read<T>(IXLWorksheet? worksheet, uint startFrom, uint skip) where T : class
     {
         if (worksheet == null) yield break;
 
@@ -80,7 +83,7 @@ public class ExcelReader
             yield return item;
     }
 
-    public IEnumerable<T> Read<T>(string? worksheetName, uint startFrom = 1, uint skip = 0) where T : class, new()
+    public IEnumerable<T> Read<T>(string? worksheetName, uint startFrom = 1, uint skip = 0) where T : class
     {
         var worksheet = xlWorkbook.Worksheets.FirstOrDefault(x => x.Name.Equals(worksheetName, StringComparison.InvariantCultureIgnoreCase));
         if (worksheet == null) yield break;
@@ -89,7 +92,7 @@ public class ExcelReader
             yield return value;
     }
 
-    public IEnumerable<T> Read<T>(int worksheetIndex = 1, uint startFrom = 1, uint skip = 0) where T : class, new()
+    public IEnumerable<T> Read<T>(int worksheetIndex = 1, uint startFrom = 1, uint skip = 0) where T : class
     {
         if (worksheetIndex > xlWorkbook.Worksheets.Count) yield break;
 
@@ -100,7 +103,7 @@ public class ExcelReader
             yield return value;
     }
 
-    public IEnumerable<T> ReadAll<T>(uint startFrom = 1, uint skip = 0) where T : class, new()
+    public IEnumerable<T> ReadAll<T>(uint startFrom = 1, uint skip = 0) where T : class
     {
         foreach (var worksheet in xlWorkbook.Worksheets)
         {

+ 3 - 3
ExcelORM/ExcelORM/ExcelWriter.cs

@@ -12,7 +12,7 @@ public class ExcelWriter
         xlWorkbook = File.Exists(path) ? new XLWorkbook(path) : new XLWorkbook();
     }
 
-    private static int GenerateHeader<T>(IXLWorksheet worksheet) where T : class, new()
+    private static int GenerateHeader<T>(IXLWorksheet worksheet) where T : class
     {
         var rowIndex = 1;
         var cellIndex = 1;
@@ -31,7 +31,7 @@ public class ExcelWriter
 
     private static Type formulaType = typeof(Formula);
 
-    private static void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append) where T : class, new()
+    private static void Write<T>(IEnumerable<T> values, IXLWorksheet worksheet, bool append) where T : class
     {
         if (!values.Any()) return;
 
@@ -73,7 +73,7 @@ public class ExcelWriter
         }
     }
 
-    public void Write<T>(IEnumerable<T> values, string? worksheetName = null, bool append = false) where T : class, new()
+    public void Write<T>(IEnumerable<T> values, string? worksheetName = null, bool append = false) where T : class
     {
         var xlWorksheet = xlWorkbook.Worksheets.FirstOrDefault(x => x.Name.Equals(worksheetName, StringComparison.InvariantCultureIgnoreCase));
         

+ 1 - 1
ExcelORM/ExcelORM/Mapping.cs

@@ -8,7 +8,7 @@ namespace ExcelORM
         public string? PropertyName { get; set; }
         public int? Position { get; set; }
 
-        public static List<Mapping>? MapProperties<T>(IXLCells? headerCells) where T : new()
+        public static List<Mapping>? MapProperties<T>(IXLCells? headerCells) where T : class
         {
             if (headerCells == null || !headerCells.Any()) return null;