mirror of
https://gitee.com/dotnetchina/MiniExcel.git
synced 2024-11-29 18:38:08 +08:00
Merge pull request #144 from isdaniel/master
Clean code use lambda instead of foreach.
This commit is contained in:
commit
81c204dd84
@ -1,5 +1,6 @@
|
||||
namespace MiniExcelLibs.Attributes
|
||||
namespace MiniExcelLibs.Attributes
|
||||
{
|
||||
using MiniExcelLibs.Utils;
|
||||
using System;
|
||||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
|
||||
public class ExcelColumnNameAttribute : Attribute
|
||||
@ -7,4 +8,12 @@
|
||||
public string ExcelColumnName { get; set; }
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -390,7 +390,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
{
|
||||
var type = typeof(T);
|
||||
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();
|
||||
foreach (var pInfo in props)
|
||||
@ -398,7 +398,7 @@ namespace MiniExcelLibs.OpenXml
|
||||
if (item.ContainsKey(pInfo.ExcelColumnName))
|
||||
{
|
||||
object newV = null;
|
||||
object itemValue = (object)item[pInfo.ExcelColumnName];
|
||||
object itemValue = item[pInfo.ExcelColumnName];
|
||||
|
||||
if (itemValue == null)
|
||||
continue;
|
||||
|
55
src/MiniExcel/Utils/AttributeExtension.cs
Normal file
55
src/MiniExcel/Utils/AttributeExtension.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -81,33 +81,32 @@
|
||||
|
||||
internal static List<ExcelCustomPropertyInfo> GetExcelCustomPropertyInfos(Type type)
|
||||
{
|
||||
var props = type.GetProperties(BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(prop => prop.GetSetMethod() != null
|
||||
&&
|
||||
!(prop.GetCustomAttribute<ExcelIgnoreAttribute>()?.ExcelIgnore == true)
|
||||
) /*ignore without set*/
|
||||
List<ExcelCustomPropertyInfo> props = GetExcelPropertyInfo(type, BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance)
|
||||
.Where(prop => prop.Property.IsSupportSetMethod() && !prop.Property.GetAttributeValue((ExcelIgnoreAttribute x) => x.ExcelIgnore))
|
||||
.ToList() /*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
|
||||
.Select(p =>
|
||||
{
|
||||
var gt = Nullable.GetUnderlyingType(p.PropertyType);
|
||||
var excelAttr = p.GetAttribute<ExcelColumnNameAttribute>();
|
||||
return new ExcelCustomPropertyInfo
|
||||
{
|
||||
Property = p,
|
||||
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>()
|
||||
|
11
src/MiniExcel/Utils/ReflactionExtension.cs
Normal file
11
src/MiniExcel/Utils/ReflactionExtension.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user