Skip to content

Commit

Permalink
Fixes issue #1421 (#1422)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Apr 19, 2024
1 parent 8315ca0 commit f49b52a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/EPPlus/Export/ToCollection/ToCollectionRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,20 @@ public void Automap<T>(T item)
else
{
//Set the default value
if(m.PropertyInfo.DeclaringType.IsValueType)
if(m.PropertyInfo.PropertyType.IsValueType)
{
m.PropertyInfo.SetValue(item, null, null);
}
else
{
m.PropertyInfo.SetValue(item, Activator.CreateInstance(m.PropertyInfo.DeclaringType), null);
try
{
m.PropertyInfo.SetValue(item, Activator.CreateInstance(m.PropertyInfo.PropertyType), null);
}
catch
{
m.PropertyInfo.SetValue(item, null, null);
}
}
}
}
Expand Down
54 changes: 53 additions & 1 deletion src/EPPlusTest/Export/ToCollection/ToCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,62 @@ public void ToCollection_AutoMapInCallback()
Assert.AreEqual(sheet.Cells["D3"].Value, list[1].TimeStamp);
}
}
[TestMethod]
[ExpectedException(typeof(EPPlusDataTypeConvertionException))]
public void ToCollection_AutoMap_EPPlusDataTypeConvertionException()
{
using (var p = new ExcelPackage())
{
var sheet = LoadTestData(p, "LoadFromCollectionAuto");
sheet.Cells["A1"].Value = "Identity";
sheet.Cells["B1"].Value = "First name";
sheet.Cells["A2"].Value = "Error";
var list = sheet.Cells["A1:E3"].ToCollectionWithMappings(x =>
{
var item = new TestDto();
x.Automap(item);
item.Category = new Category() { CatId = x.GetValue<int>("CategoryId") };
item.FormattedRatio = x.GetText("Ratio");
item.FormattedTimeStamp = x.GetText("TimeStamp");
return item;
}, x => x.HeaderRow = 0);
}
}
[TestMethod]
public void ToCollection_AutoMapInCallback_SetDefaultValueOnConversionError()
{
using (var p = new ExcelPackage())
{
var sheet = LoadTestData(p, "LoadFromCollectionAuto");
sheet.Cells["A1"].Value = "Identity";
sheet.Cells["B1"].Value = "First name";
sheet.Cells["A2"].Value = "Error";
var list = sheet.Cells["A1:E3"].ToCollectionWithMappings(x =>
{
var item = new TestDto();
x.Automap(item);
item.Category = new Category() { CatId = x.GetValue<int>("CategoryId") };
item.FormattedRatio = x.GetText("Ratio");
item.FormattedTimeStamp = x.GetText("TimeStamp");
return item;
}, x => { x.HeaderRow = 0; x.ConversionFailureStrategy = ToCollectionConversionFailureStrategy.SetDefaultValue; });

Assert.AreEqual(2, list.Count);
Assert.AreEqual(default(int), list[0].Id); //sheet.Cells["A2"].Value is invalid for int as it contains a string
Assert.AreEqual(sheet.Cells["B2"].Text, list[0].Name);
Assert.AreEqual(sheet.Cells["C2"].Value, list[0].Ratio);
Assert.AreEqual(sheet.Cells["D2"].Value, list[0].TimeStamp);

Assert.AreEqual(sheet.Cells["A3"].Value, list[1].Id);
Assert.AreEqual(sheet.Cells["B3"].Text, list[1].Name);
Assert.AreEqual(sheet.Cells["C3"].Value, list[1].Ratio);
Assert.AreEqual(sheet.Cells["D3"].Value, list[1].TimeStamp);
}
}

#endif
#endregion
#region Table
#region Table
[TestMethod]
public void ToCollectionTable_AutoMap()
{
Expand Down

0 comments on commit f49b52a

Please sign in to comment.