diff --git a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs index 651530d..645859d 100644 --- a/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs +++ b/Npoi.Mapper/src/Npoi.Mapper/MapHelper.cs @@ -538,10 +538,13 @@ public static (PropertyInfo propertyInfo, string fullPath) GetPropertyInfo(st { var memberAccess = body as MemberExpression ?? (MemberExpression)((UnaryExpression)body).Operand; - newBody = Expression.Condition( - Expression.Equal(memberAccess.Expression, Expression.Constant(null)), - Expression.Convert(Expression.Constant(null), ObjectType), - newBody); + if (!memberAccess.Expression.Type.IsValueType) + { + newBody = Expression.Condition( + Expression.Equal(memberAccess.Expression, Expression.Constant(null)), + Expression.Convert(Expression.Constant(null), ObjectType), + newBody); + } body = memberAccess.Expression; } @@ -581,11 +584,15 @@ public static (PropertyInfo propertyInfo, string fullPath) GetPropertyInfo(st { var memberAccess = innerBody as MemberExpression ?? (MemberExpression)((UnaryExpression)innerBody).Operand; - var assignNewObjExpression = Expression.IfThen( - Expression.Equal(memberAccess, Expression.Constant(null)), - Expression.Assign(memberAccess, Expression.New(memberAccess.Type))); + if (!memberAccess.Type.IsValueType) + { + var ifNullAssignNew = Expression.IfThen( + Expression.Equal(memberAccess, Expression.Constant(null)), + Expression.Assign(memberAccess, Expression.New(memberAccess.Type))); + + statements.Add(ifNullAssignNew); + } - statements.Add(assignNewObjExpression); innerBody = memberAccess.Expression; } diff --git a/Npoi.Mapper/src/Npoi.Mapper/Mapper.cs b/Npoi.Mapper/src/Npoi.Mapper/Mapper.cs index 02d197b..3537c65 100644 --- a/Npoi.Mapper/src/Npoi.Mapper/Mapper.cs +++ b/Npoi.Mapper/src/Npoi.Mapper/Mapper.cs @@ -812,13 +812,11 @@ void ColumnFailed(IColumnInfo column, string message) if (MapHelper.TryConvertType(valueObj, column, UseDefaultValueAttribute, out var result)) { column.Attribute.GetSetterOrDefault(target)?.Invoke(target, result); - //column.Attribute.Property.SetValue(target, result, null); } else { ColumnFailed(column, "Cannot convert value to the property type!"); } - //var value = Convert.ChangeType(valueObj, column.Attribute.PropertyUnderlyingType ?? propertyType); } } catch (Exception e) @@ -909,8 +907,6 @@ private void Put(ISheet sheet, IEnumerable objects, bool overwrite) foreach (var column in columns) { - //var pi = column.Attribute.Property; - //var value = pi?.GetValue(o, null); var value = column.Attribute.GetGetterOrDefault(o)?.Invoke(o); var cell = row.GetCell(column.Attribute.Index, MissingCellPolicy.CREATE_NULL_AS_BLANK); diff --git a/Npoi.Mapper/test/NestedPropertyTests.cs b/Npoi.Mapper/test/NestedPropertyTests.cs index b53ff3e..a851ef0 100644 --- a/Npoi.Mapper/test/NestedPropertyTests.cs +++ b/Npoi.Mapper/test/NestedPropertyTests.cs @@ -1,4 +1,5 @@ -using System.IO; +using System; +using System.IO; using System.Linq; using Npoi.Mapper; using NUnit.Framework; @@ -19,6 +20,7 @@ private class CustomerBilling { public BillingAddress Address { get; set; } public BillingContact Contact { get; set; } + public MyStruct Dates; } private class BillingAddress @@ -31,6 +33,11 @@ private class BillingContact public string Name { get; set; } } + private struct MyStruct + { + public DateTimeOffset Dto { get; set; } + } + [Test] public void ImportWithNestedPropertiesTest() { @@ -39,6 +46,7 @@ public void ImportWithNestedPropertiesTest() const string contactName = "tian"; const int customerAge = 33; const int addressId = 4321; + var date = DateTime.Now; var workbook = GetBlankWorkbook(); var sheet = workbook.GetSheetAt(0); var row0 = sheet.CreateRow(0); @@ -49,16 +57,19 @@ public void ImportWithNestedPropertiesTest() row0.CreateCell(1).SetCellValue("customer age"); row0.CreateCell(2).SetCellValue("contact name"); row0.CreateCell(3).SetCellValue("address id"); + row0.CreateCell(4).SetCellValue("birth date"); row1.CreateCell(0).SetCellValue(customerName); row1.CreateCell(1).SetCellValue(customerAge); row1.CreateCell(2).SetCellValue(contactName); row1.CreateCell(3).SetCellValue(addressId); + row1.CreateCell(4).SetCellValue(date); row2.CreateCell(0).SetCellValue(""); row2.CreateCell(1).SetCellValue(customerAge.ToString()); row2.CreateCell(2).SetCellValue((string)null); row2.CreateCell(3).SetCellValue(""); + row2.CreateCell(4).SetCellValue(""); // Act var mapper = new Mapper(workbook); @@ -66,6 +77,7 @@ public void ImportWithNestedPropertiesTest() mapper.Map(1, c => c.Age); mapper.Map(2, c => c.Billing.Contact.Name); mapper.Map(3, c => c.Billing.Address.AddressId); + mapper.Map(4, c => c.Billing.Dates.Dto); var objs = mapper.Take().ToList(); @@ -75,11 +87,13 @@ public void ImportWithNestedPropertiesTest() Assert.AreEqual(customerAge, objs[0].Value.Age); Assert.AreEqual(contactName, objs[0].Value.Billing.Contact.Name); Assert.AreEqual(addressId, objs[0].Value.Billing.Address.AddressId); + Assert.AreEqual(date.Date, objs[0].Value.Billing.Dates.Dto.Date); Assert.AreEqual("", objs[1].Value.Name); Assert.AreEqual(customerAge, objs[1].Value.Age); Assert.AreEqual("", objs[1].Value.Billing.Contact.Name); Assert.AreEqual(null, objs[1].Value.Billing.Address); + Assert.AreEqual(DateTime.MinValue, objs[1].Value.Billing.Dates.Dto.Date); } [Test] @@ -93,6 +107,7 @@ public void ExportWithNestedPropertiesTest() const int customerAge = 33; const int addressId = 4321; const int addressId2 = 3333; + var date = DateTime.Now; var customer1 = new Customer { Age = customerAge, @@ -101,6 +116,7 @@ public void ExportWithNestedPropertiesTest() { Address = new BillingAddress { AddressId = addressId }, Contact = new BillingContact { Name = contactName }, + Dates = new MyStruct { Dto = date }, }, }; var customer2 = new Customer @@ -120,6 +136,7 @@ public void ExportWithNestedPropertiesTest() mapper.Map(1, c => c.Age); mapper.Map(2, c => c.Billing.Contact.Name); mapper.Map(3, c => c.Billing.Address.AddressId); + mapper.Map(4, c => c.Billing.Dates.Dto); mapper.Save(fileName, entities, false); @@ -134,15 +151,18 @@ public void ExportWithNestedPropertiesTest() Assert.AreEqual(nameof(Customer.Age), row0.GetCell(1).StringCellValue); Assert.AreEqual(nameof(Customer.Billing.Contact.Name), row0.GetCell(2).StringCellValue); Assert.AreEqual(nameof(Customer.Billing.Address.AddressId), row0.GetCell(3).StringCellValue); + Assert.AreEqual(nameof(Customer.Billing.Dates.Dto), row0.GetCell(4).StringCellValue); Assert.AreEqual(customer1.Name, row1.GetCell(0).StringCellValue); Assert.AreEqual(customer1.Age, row1.GetCell(1).NumericCellValue); Assert.AreEqual(customer1.Billing.Contact.Name, row1.GetCell(2).StringCellValue); Assert.AreEqual(customer1.Billing.Address.AddressId, row1.GetCell(3).NumericCellValue); + Assert.AreEqual(customer1.Billing.Dates.Dto.Date, DateTimeOffset.Parse(row1.GetCell(4).StringCellValue).Date); Assert.AreEqual(customer2.Name ?? "", row2.GetCell(0).StringCellValue); Assert.AreEqual(customer2.Age ?? 0.0, row2.GetCell(1).NumericCellValue); Assert.AreEqual("", row2.GetCell(2).StringCellValue); Assert.AreEqual(customer2.Billing.Address.AddressId, row2.GetCell(3).NumericCellValue); + Assert.AreEqual(customer2.Billing.Dates.Dto.Date, DateTimeOffset.Parse(row2.GetCell(4).StringCellValue).Date); } }