Skip to content

Commit 938ac80

Browse files
Added custom exception for indexers being treated as properties during serialization (#884)
1 parent f6b6059 commit 938ac80

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MiniExcelLib.Core.Exceptions;
2+
3+
public class MiniExcelNotSerializableException(string message, MemberInfo member)
4+
: InvalidOperationException(message)
5+
{
6+
public MemberInfo Member { get; } = member;
7+
}

src/MiniExcel.Core/Reflection/MiniExcelProperty.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using MiniExcelLib.Core.Exceptions;
2+
13
namespace MiniExcelLib.Core.Reflection;
24

35
public abstract class Member;
@@ -12,6 +14,12 @@ public MiniExcelProperty(PropertyInfo property)
1214
Name = property.Name;
1315
Info = property;
1416

17+
if (property.GetIndexParameters().Length != 0)
18+
{
19+
const string msg = "Types containing indexers cannot be serialized. Please remove them or decorate them with MiniExcelIgnoreAttribute.";
20+
throw new MiniExcelNotSerializableException(msg, property);
21+
}
22+
1523
if (property.CanRead)
1624
{
1725
CanRead = true;

tests/MiniExcel.Core.Tests/MiniExcelIssueTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3710,4 +3710,22 @@ public void TestIssue876()
37103710
_excelExporter.Export(outputPath.ToString(), sheets);
37113711
});
37123712
}
3713+
3714+
private class Issue880
3715+
{
3716+
public string Test { get; set; }
3717+
public string this[int i] => "";
3718+
}
3719+
3720+
[Fact]
3721+
public void TestIssue880_ShouldThrowNotSerializableException()
3722+
{
3723+
Issue880[] toExport = [new() { Test = "test" }];
3724+
3725+
Assert.Throws<MiniExcelNotSerializableException>(() =>
3726+
{
3727+
using var ms = new MemoryStream();
3728+
_excelExporter.Export(ms, toExport);
3729+
});
3730+
}
37133731
}

0 commit comments

Comments
 (0)