Example #1
0
        /// <summary>
        /// 获取映射数据
        /// </summary>
        /// <param name="property">属性信息</param>
        /// <returns></returns>
        public string GetMappingColumnName(PropertyInfo property)
        {
            // 获取显示名称

            var columnName = string.Empty;

            if (NameMapping != null)
            {
                columnName = NameMapping.GetValueByKey(property.Name);
            }

            // 从 Attribute 中自动获取对应的列名
            if (string.IsNullOrEmpty(columnName) && AutoResolveColumnName)
            {
                var descriptionAttribute = property.GetAttribute <DescriptionAttribute>();
                if (descriptionAttribute == null)
                {
                    var displayAttr = property.GetAttribute <DisplayAttribute>();
                    if (displayAttr != null)
                    {
                        columnName = displayAttr.Name;
                    }
                }
                else
                {
                    columnName = descriptionAttribute.Description;
                }
            }

            return(columnName);
        }
Example #2
0
        /// <summary>
        /// DataRow 转换成实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="row">数据行</param>
        /// <param name="errors">错误我</param>
        /// <returns></returns>
        public T GetEntity(DataRow row, out List <KeyValuePair <string, string> > errors)
        {
            var entity     = new T();
            var properties = typeof(T).GetProperties();

            errors = new List <KeyValuePair <string, string> >();
            var additionalData = GetAdditionalData(row);

            // 对实体的每个属性赋值
            foreach (var property in properties)
            {
                var columnName = GetMappingColumnName(property);  // 列名
                try
                {
                    var value     = GetPropertyValue(row, property, additionalData);
                    var safeValue = ValueHelper.GetSafeValue(value, property.PropertyType);
                    if (value != null && safeValue != null)
                    {
                        property.SetValue(entity, safeValue, null);
                    }
                }
                catch (LogicException ex)
                {
                    string key;
                    if (string.IsNullOrEmpty(ex.Key))
                    {
                        key = columnName;
                    }
                    else
                    {
                        key = NameMapping.GetValueByKey(ex.Key);
                        if (string.IsNullOrEmpty(key))
                        {
                            key = ex.Key;
                        }
                    }

                    var error = new KeyValuePair <string, string>(key, ex.Message);
                    errors.Add(error);
                }
                catch (Exception ex)
                {
                    var key = columnName;

                    var error = new KeyValuePair <string, string>(key, ex.Message);
                    errors.Add(error);
                }
            }

            return(entity);
        }