Merge pull request #144 from isdaniel/master

Clean code use lambda instead of foreach.
This commit is contained in:
Wei 2021-04-03 18:04:45 +08:00 committed by GitHub
commit 81c204dd84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 95 additions and 21 deletions

View File

@ -1,5 +1,6 @@
namespace MiniExcelLibs.Attributes namespace MiniExcelLibs.Attributes
{ {
using MiniExcelLibs.Utils;
using System; using System;
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnNameAttribute : Attribute public class ExcelColumnNameAttribute : Attribute
@ -7,4 +8,12 @@
public string ExcelColumnName { get; set; } public string ExcelColumnName { get; set; }
public ExcelColumnNameAttribute(string excelColumnName) => ExcelColumnName = excelColumnName; public ExcelColumnNameAttribute(string excelColumnName) => ExcelColumnName = excelColumnName;
} }
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ExcelColumnIndexAttribute : Attribute
{
public int ExcelColumnIndex { get; set; }
public ExcelColumnIndexAttribute(string columnName) => ExcelColumnIndex = Helpers.GetColumnIndex(columnName);
public ExcelColumnIndexAttribute(int columnIndex) => ExcelColumnIndex = columnIndex;
}
} }

View File

@ -390,7 +390,7 @@ namespace MiniExcelLibs.OpenXml
{ {
var type = typeof(T); var type = typeof(T);
var props = Helpers.GetExcelCustomPropertyInfos(type); var props = Helpers.GetExcelCustomPropertyInfos(type);
foreach (var item in new ExcelOpenXmlSheetReader().Query(stream, true)) foreach (var item in Query(stream, true))
{ {
var v = new T(); var v = new T();
foreach (var pInfo in props) foreach (var pInfo in props)
@ -398,7 +398,7 @@ namespace MiniExcelLibs.OpenXml
if (item.ContainsKey(pInfo.ExcelColumnName)) if (item.ContainsKey(pInfo.ExcelColumnName))
{ {
object newV = null; object newV = null;
object itemValue = (object)item[pInfo.ExcelColumnName]; object itemValue = item[pInfo.ExcelColumnName];
if (itemValue == null) if (itemValue == null)
continue; continue;

View File

@ -0,0 +1,55 @@
namespace MiniExcelLibs.Utils
{
using System;
using System.Linq;
using System.Reflection;
internal static class AttributeExtension
{
internal static TValue GetAttributeValue<TAttribute, TValue>(
this Type attrType,
Func<TAttribute, TValue> selector) where TAttribute : Attribute
{
var attr = attrType.GetCustomAttributes(typeof(TAttribute), true).FirstOrDefault() as TAttribute;
return GetValueOrDefault(selector, attr);
}
private static TValue GetValueOrDefault<TAttribute, TValue>
(Func<TAttribute, TValue> selector, TAttribute attr)
where TAttribute : Attribute
{
if (attr != null)
{
return selector(attr);
}
return default(TValue);
}
internal static TAttribute GetAttribute<TAttribute>(
this PropertyInfo prop,
bool isInherit = true
)
where TAttribute : Attribute
{
return GetAttributeValue(prop, (TAttribute attr) => attr, isInherit);
}
internal static TValue GetAttributeValue<TAttribute, TValue>(
this PropertyInfo prop,
Func<TAttribute, TValue> selector,
bool isInherit = true
)
where TAttribute : Attribute
{
TAttribute attr = Attribute.GetCustomAttribute(prop, typeof(TAttribute), isInherit) as TAttribute;
return GetValueOrDefault(selector, attr);
}
internal static bool IsUseAttribute<TAttribute>(this PropertyInfo prop)
{
return Attribute.GetCustomAttribute(prop, typeof(TAttribute)) != null;
}
}
}

View File

@ -81,33 +81,32 @@
internal static List<ExcelCustomPropertyInfo> GetExcelCustomPropertyInfos(Type type) internal static List<ExcelCustomPropertyInfo> GetExcelCustomPropertyInfos(Type type)
{ {
var props = type.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance) List<ExcelCustomPropertyInfo> props = GetExcelPropertyInfo(type, BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.GetSetMethod() != null .Where(prop => prop.Property.IsSupportSetMethod() && !prop.Property.GetAttributeValue((ExcelIgnoreAttribute x) => x.ExcelIgnore))
&& .ToList() /*ignore without set*/;
!(prop.GetCustomAttribute<ExcelIgnoreAttribute>()?.ExcelIgnore == true)
) /*ignore without set*/ if (props.Count == 0)
throw new InvalidOperationException($"{type.Name} un-ignore properties count can't be 0");
return props;
}
private static IEnumerable<ExcelCustomPropertyInfo> GetExcelPropertyInfo(Type type, BindingFlags bindingFlags)
{
return type.GetProperties(bindingFlags)
// solve : https://github.com/shps951023/MiniExcel/issues/138 // solve : https://github.com/shps951023/MiniExcel/issues/138
.Select(p => .Select(p =>
{ {
var gt = Nullable.GetUnderlyingType(p.PropertyType); var gt = Nullable.GetUnderlyingType(p.PropertyType);
var excelAttr = p.GetAttribute<ExcelColumnNameAttribute>();
return new ExcelCustomPropertyInfo return new ExcelCustomPropertyInfo
{ {
Property = p, Property = p,
ExcludeNullableType = gt ?? p.PropertyType, ExcludeNullableType = gt ?? p.PropertyType,
Nullable = gt != null ? true : false Nullable = gt != null ? true : false,
ExcelColumnName = excelAttr?.ExcelColumnName ?? p.Name
}; };
}) });
.ToList();
if (props.Count == 0)
throw new InvalidOperationException($"{type.Name} un-ignore properties count can't be 0");
foreach (var cp in props)
{
cp.ExcelColumnName = cp.Property.GetCustomAttribute<ExcelColumnNameAttribute>()?.ExcelColumnName;
if (cp.ExcelColumnName == null)
cp.ExcelColumnName = cp.Property.Name;
}
return props;
} }
internal static bool IsDapperRows<T>() internal static bool IsDapperRows<T>()

View File

@ -0,0 +1,11 @@
namespace MiniExcelLibs.Utils
{
using System.Reflection;
internal static class ReflactionExtension {
internal static bool IsSupportSetMethod(this PropertyInfo propertyInfo) {
return propertyInfo.GetSetMethod() != null;
}
}
}