public virtual List <T> DataTableToList <T>(string filePath, string sheetName) where T : IExeclImport, new() { List <T> listEntity = new List <T>(); DataTable dt = excelReader.ReaderExcel(filePath, sheetName); if (dt.Rows.Count > this.MaxLeng) { this.ErrorMsg = $"超过最大条数{MaxLeng}"; return(null); } string TName = GetTName <T>(); string ExeclMappingPropertyInfoKey = $"{TName}_ExeclMappingPropertys"; #region 获取实体类映射集合 IEnumerable <PropertyInfo> PropertyInfos = CacheHelper.GetCache <IEnumerable <PropertyInfo> >(ExeclMappingPropertyInfoKey); if (PropertyInfos == null) { PropertyInfos = typeof(T).GetProperties().Where(m => m.GetCustomAttributes(typeof(ExeclMappingAttribute), true) != null); CacheHelper.SetCache(ExeclMappingPropertyInfoKey, PropertyInfos); } #endregion if (PropertyInfos == null || PropertyInfos.Count() <= 0) { this.ErrorMsg = $"传入实体无Execl映射信息"; return(null); } foreach (DataRow row in dt.Rows) { T t = new T(); foreach (PropertyInfo Prty in PropertyInfos) { ExeclMappingAttribute PrtyExeclMapping = Prty.GetCustomAttribute <ExeclMappingAttribute>(true); if (dt.Columns.Contains(PrtyExeclMapping.ExeclHeadName)) { // 判断此属性是否有Setter if (!Prty.CanWrite) { continue; //该属性不可写,直接跳出 } //取值 object value = row[PrtyExeclMapping.ExeclHeadName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value && value != null && !string.IsNullOrEmpty(value.ToString())) { ConvertValueAttribute appointValueAttribute = Prty.GetCustomAttribute <ConvertValueAttribute>(true); if (appointValueAttribute != null) { value = appointValueAttribute.GetConvertValueValue(value); } object objValue = GetValue(Prty, value); Prty.SetValue(t, objValue, null); } } } listEntity.Add(t); } return(listEntity); }
public virtual List <T> DataTableToList <T>(string filePath, string sheetName) where T : IExeclImport, new() { List <T> listEntity = new List <T>(); DataTable dt = excelReader.ReaderExcel(filePath, sheetName); if (dt.Rows.Count > this.MaxLeng) { this.ErrorMsg = $"超过最大条数{MaxLeng}"; return(null); } string TName = GetTName <T>(); Type TType = typeof(T); #region 获取实体类映射集合 string ExeclMappingPropertyInfosKey = $"{TName}_ExeclMappingPropertys"; IEnumerable <PropertyInfo> PropertyInfos = CacheHelper.GetCache <IEnumerable <PropertyInfo> >(ExeclMappingPropertyInfosKey); if (PropertyInfos == null) { PropertyInfos = TType.GetProperties().Where(m => m.GetCustomAttribute <ExeclMappingAttribute>(true) != null); if (PropertyInfos != null) { CacheHelper.SetCache(ExeclMappingPropertyInfosKey, PropertyInfos); } } #endregion if (PropertyInfos == null || PropertyInfos.Count() <= 0) { this.ErrorMsg = $"传入实体无Execl映射信息"; return(null); } foreach (DataRow row in dt.Rows) { T t = new T(); foreach (PropertyInfo Prty in PropertyInfos) { #region 获取指定字段ExeclMappingAttribute string ExeclMappingPropertyKey = $"{TName}_{Prty.Name}_ExeclMappingPropertyKey"; string ExeclMappingPropertyKeyNoHave = $"{TName}_{Prty.Name}_ExeclMappingPropertyKeyNoHave"; ExeclMappingAttribute PrtyExeclMapping = CacheHelper.GetCache <ExeclMappingAttribute>(ExeclMappingPropertyKey); if (PrtyExeclMapping == null && !CacheHelper.IsExist(ExeclMappingPropertyKeyNoHave)) { PrtyExeclMapping = Prty.GetCustomAttribute <ExeclMappingAttribute>(true); if (PrtyExeclMapping != null) { CacheHelper.SetCache(ExeclMappingPropertyKey, PrtyExeclMapping); } else { CacheHelper.SetCache(ExeclMappingPropertyKeyNoHave, true); } } #endregion if (PrtyExeclMapping != null && dt.Columns.Contains(PrtyExeclMapping.ExeclHeadName)) { // 判断此属性是否有Setter if (!Prty.CanWrite) { continue; //该属性不可写,直接跳出 } //取值 object value = row[PrtyExeclMapping.ExeclHeadName]; //如果非空,则赋给对象的属性 if (value != DBNull.Value && value != null && !string.IsNullOrEmpty(value.ToString())) { #region 获取指定字段ConvertValueAttribute string ConvertValuePropertyKey = $"{TName}_{Prty.Name}_ConvertValuePropertyKey"; string ConvertValuePropertyKeyNoHave = $"{TName}_{Prty.Name}_ConvertValuePropertyKeyNoHave"; ConvertValueAttribute appointValueAttribute = CacheHelper.GetCache <ConvertValueAttribute>(ConvertValuePropertyKey); if (appointValueAttribute == null && !CacheHelper.IsExist(ConvertValuePropertyKeyNoHave)) { appointValueAttribute = Prty.GetCustomAttribute <ConvertValueAttribute>(true); if (appointValueAttribute != null) { CacheHelper.SetCache(ConvertValuePropertyKey, appointValueAttribute); } else { CacheHelper.SetCache(ConvertValuePropertyKeyNoHave, true); } } #endregion if (appointValueAttribute != null) { value = appointValueAttribute.GetConvertValueValue(value); } ExcelNumberAttribute ss = Prty.GetCustomAttributes <ExcelNumberAttribute>(true); #region 字段验证BaseValidationAttribute string BaseValidationPropertyKey = $"{TName}_{Prty.Name}_BaseValidationPropertyKey"; string BaseValidationPropertyKeyNoHave = $"{TName}_{Prty.Name}_BaseValidationPropertyKeyNoHave"; IEnumerable <ExcelBaseValidationAttribute> baseValidationAttributeArray = CacheHelper.GetCache <IEnumerable <ExcelBaseValidationAttribute> >(BaseValidationPropertyKey); if (baseValidationAttributeArray == null && !CacheHelper.IsExist(BaseValidationPropertyKeyNoHave)) { baseValidationAttributeArray = Prty.GetCustomAttributes <ExcelBaseValidationAttribute>(true); if (baseValidationAttributeArray != null) { CacheHelper.SetCache(BaseValidationPropertyKey, baseValidationAttributeArray); } else { CacheHelper.SetCache(BaseValidationPropertyKeyNoHave, true); } } #endregion if (baseValidationAttributeArray != null && baseValidationAttributeArray.Count() > 0) { foreach (ExcelBaseValidationAttribute baseValidationAttribute in baseValidationAttributeArray) { if (!baseValidationAttribute.IsValid(value)) { PropertyInfo ExeclRowErrorMsgProperty = TType.GetProperty("ExeclRowErrorMsg"); if (ExeclRowErrorMsgProperty != null) { ExeclRowErrorMsgProperty.SetValue(t, string.IsNullOrEmpty(baseValidationAttribute.ErrorMsg)? baseValidationAttribute.ErrorMsg:$"{baseValidationAttribute.GetType().Name} 验证未通过" , null); } } } } object objValue = GetValue(Prty, value); Prty.SetValue(t, objValue, null); } } } listEntity.Add(t); } return(listEntity); }
public MethodProxy(Context context, MethodBase methodBase, object hardTarget) : base(context) { _method = methodBase; _hardTarget = hardTarget; _parameters = methodBase.GetParameters(); _strictConversion = methodBase.IsDefined(typeof(StrictConversionAttribute), true); _name = methodBase.Name; if (methodBase.IsDefined(typeof(JavaScriptNameAttribute), false)) { _name = (methodBase.GetCustomAttributes(typeof(JavaScriptNameAttribute), false).First() as JavaScriptNameAttribute).Name; if (_name.StartsWith("@@")) { _name = _name.Substring(2); } } if (_length == null) { _length = new Number(0) { _attributes = JSValueAttributesInternal.ReadOnly | JSValueAttributesInternal.DoNotDelete | JSValueAttributesInternal.DoNotEnumerate | JSValueAttributesInternal.SystemObject } } ; if (methodBase.IsDefined(typeof(ArgumentsCountAttribute), false)) { var argsCountAttribute = methodBase.GetCustomAttributes(typeof(ArgumentsCountAttribute), false).First() as ArgumentsCountAttribute; _length._iValue = argsCountAttribute.Count; } else { _length._iValue = _parameters.Length; } for (int i = 0; i < _parameters.Length; i++) { if (_parameters[i].IsDefined(typeof(ConvertValueAttribute), false)) { var t = _parameters[i].GetCustomAttributes(typeof(ConvertValueAttribute), false).First(); if (_paramsConverters == null) { _paramsConverters = new ConvertValueAttribute[_parameters.Length]; } _paramsConverters[i] = t as ConvertValueAttribute; } } var methodInfo = methodBase as MethodInfo; if (methodInfo != null) { _returnConverter = methodInfo.ReturnParameter.GetCustomAttribute(typeof(ConvertValueAttribute), false) as ConvertValueAttribute; _forceInstance = methodBase.IsDefined(typeof(InstanceMemberAttribute), false); if (_forceInstance) { if (!methodInfo.IsStatic || (_parameters.Length == 0) || (_parameters.Length > 2) || (_parameters[0].ParameterType != typeof(JSValue)) || (_parameters.Length > 1 && _parameters[1].ParameterType != typeof(Arguments))) { throw new ArgumentException("Force-instance method \"" + methodBase + "\" has invalid signature"); } _raw = true; } if (!WrapperCache.TryGetValue(methodBase, out _fastWrapper)) { WrapperCache[methodBase] = _fastWrapper = makeFastWrapper(methodInfo); } _raw |= _parameters.Length == 0 || (_parameters.Length == 1 && _parameters[0].ParameterType == typeof(Arguments)); RequireNewKeywordLevel = RequireNewKeywordLevel.WithoutNewOnly; } else if (methodBase is ConstructorInfo) { if (!WrapperCache.TryGetValue(methodBase, out _fastWrapper)) { WrapperCache[methodBase] = _fastWrapper = makeFastWrapper(methodBase as ConstructorInfo); } _raw |= _parameters.Length == 0 || (_parameters.Length == 1 && _parameters[0].ParameterType == typeof(Arguments)); } else { throw new NotImplementedException(); } }