Piotr Czajkowski 1 місяць тому
батько
коміт
eb4dd512a3

+ 7 - 0
ExcelORM/ExcelORM/Attributes/SkipAttribute.cs

@@ -0,0 +1,7 @@
+namespace ExcelORM.Attributes
+{
+    [AttributeUsage(AttributeTargets.Property)]
+    public class SkipAttribute : Attribute
+    {
+    }
+}

+ 3 - 1
ExcelORM/ExcelORM/Mapping.cs

@@ -16,6 +16,8 @@ namespace ExcelORM
             var properties = typeof(T).GetProperties();
             foreach (var property in properties)
             {
+                if (property.GetCustomAttributes(typeof(SkipAttribute), false).FirstOrDefault() != null) continue;
+
                 int? position;
 
                 if (property.GetCustomAttributes(typeof(ColumnAttribute), false).FirstOrDefault() is ColumnAttribute { Names.Length: > 0 } attribute)
@@ -29,7 +31,7 @@ namespace ExcelORM
                 map.Add(new Mapping { PropertyName = property.Name, Position = position });
             }
 
-            return map.Count == properties.Length ? map : null;
+            return map.Count > 0 ? map : null;
         }
     }
 }

+ 10 - 0
ExcelORM/ExcelORMTests/ReaderTests.cs

@@ -75,4 +75,14 @@ public class ReaderTests
         var results = reader.Read<TestTypes>().ToArray();
         Assert.NotEmpty(results);
     }
+
+    [Fact]
+    public void ReadDifferentTypesWithSkip()
+    {
+        var reader = new ExcelReader(DifferentTypesFile);
+        var results = reader.Read<TestSkip>().ToArray();
+        Assert.NotEmpty(results);
+        Assert.All(results, (x) => Assert.Null(x.Text));
+        Assert.NotNull(results.FirstOrDefault(x => x.Date != null));
+    }
 }

+ 11 - 0
ExcelORM/ExcelORMTests/TestSkip.cs

@@ -0,0 +1,11 @@
+using ExcelORM.Attributes;
+
+namespace ExcelORMTests
+{
+    public record TestSkip
+    {
+        [Skip]
+        public string? Text { get; set; }
+        public DateTime? Date { get; set; }
+    }
+}