Update TypeHelper.cs

add all type nullable support

Signed-off-by: Gary Jia <35099424+jiaguangli@users.noreply.github.com>
This commit is contained in:
Gary Jia 2024-07-08 10:00:40 +08:00 committed by GitHub
parent 5230c8d273
commit d786ba36ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -75,7 +75,11 @@
private static object TypeMappingImpl<T>(T v, ExcelColumnInfo pInfo, ref object newValue, object itemValue, Configuration _config) where T : class, new()
{
if (pInfo.ExcludeNullableType == typeof(Guid))
if (pInfo.Nullable && string.IsNullOrWhiteSpace(itemValue?.ToString()))
{
newValue = null;
}
else if (pInfo.ExcludeNullableType == typeof(Guid))
{
newValue = Guid.Parse(itemValue.ToString());
}
@ -122,8 +126,6 @@
newValue = _v2;
else if (double.TryParse(vs, NumberStyles.None, CultureInfo.InvariantCulture, out var _d))
newValue = DateTimeHelper.FromOADate(_d);
else if (pInfo.Nullable && string.IsNullOrWhiteSpace(vs))
newValue = null;
else
throw new InvalidCastException($"{vs} can't cast to datetime");
}
@ -151,15 +153,8 @@
}
else
{
if (pInfo.Nullable && string.IsNullOrWhiteSpace(itemValue?.ToString()))
{
newValue = null;
}
else
{
// Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138
newValue = Convert.ChangeType(itemValue, pInfo.ExcludeNullableType, _config.Culture);
}
// Use pInfo.ExcludeNullableType to resolve : https://github.com/shps951023/MiniExcel/issues/138
newValue = Convert.ChangeType(itemValue, pInfo.ExcludeNullableType, _config.Culture);
}
pInfo.Property.SetValue(v, newValue);