diff --git a/src/MiniExcel/Attributes/QueryFromDescriptionAttribute.cs b/src/MiniExcel/Attributes/QueryFromDescriptionAttribute.cs new file mode 100644 index 0000000000000000000000000000000000000000..11bea25db2ee4430c4b0fc4f6798209b767765b9 --- /dev/null +++ b/src/MiniExcel/Attributes/QueryFromDescriptionAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace MiniExcelLibs.Attributes +{ + [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] + public class QueryFromDescriptionAttribute : Attribute + { + } +} \ No newline at end of file diff --git a/src/MiniExcel/Utils/TypeHelper.cs b/src/MiniExcel/Utils/TypeHelper.cs index ff28ddcbebab9a46e44c874c2cc00ea2fd54a11d..0cf8b5e14305865fa074730ea3f9f591269bae7c 100644 --- a/src/MiniExcel/Utils/TypeHelper.cs +++ b/src/MiniExcel/Utils/TypeHelper.cs @@ -4,16 +4,15 @@ using MiniExcelLibs.Exceptions; using System; using System.Collections.Generic; + using System.ComponentModel; using System.Data; - using System.Dynamic; using System.Globalization; - using System.IO; using System.Linq; using System.Reflection; internal static partial class TypeHelper { - public static IEnumerable> ConvertToEnumerableDictionary(IDataReader reader) + public static IEnumerable> ConvertToEnumerableDictionary(IDataReader reader) { while (reader.Read()) { @@ -109,7 +108,7 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals var vs = itemValue?.ToString(); if (pInfo.ExcelFormat != null) { - if( pInfo.Property.PropertyType == typeof(DateTimeOffset) && DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2)) + if (pInfo.Property.PropertyType == typeof(DateTimeOffset) && DateTimeOffset.TryParseExact(vs, pInfo.ExcelFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var _v2)) { newValue = _v2; } @@ -143,7 +142,14 @@ public static bool IsNumericType(Type type, bool isNullableUnderlyingType = fals } else if (pInfo.Property.PropertyType.IsEnum) { - newValue = Enum.Parse(pInfo.Property.PropertyType, itemValue?.ToString(), true); + var enumFromDesc = pInfo.Property.GetAttribute(); + if (enumFromDesc != null) + { + var fieldInfo = pInfo.Property.PropertyType.GetFields().FirstOrDefault(e => e.GetCustomAttribute(false)?.Description == itemValue?.ToString()); + if (fieldInfo != null) newValue = Enum.Parse(pInfo.Property.PropertyType, fieldInfo.Name, true); + else Enum.Parse(pInfo.Property.PropertyType, itemValue?.ToString(), true); + } + else newValue = Enum.Parse(pInfo.Property.PropertyType, itemValue?.ToString(), true); } else {