예제 #1
0
        private IEnumerable <PropertySetter> GetSettersForProperty <T>(PropertyInfo propertyInfo, int index,
                                                                       IExcelToEnumerableOptions <T> options)
        {
            if (propertyInfo.PropertyType != typeof(string) &&
                typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
            {
                if (options.CollectionConfigurations == null ||
                    !options.CollectionConfigurations.ContainsKey(propertyInfo.Name))
                {
                    return(new PropertySetter[0]);
                }

                return(GetSettersForEnumerable(propertyInfo, index, options));
            }

            var setter = GetterSetterHelpers.GetSetter(propertyInfo);
            var getter = options.UniqueProperties != null && options.UniqueProperties.Contains(propertyInfo.Name)
                ? GetterSetterHelpers.GetGetter(propertyInfo)
                : null;
            var fromCellSetter = new PropertySetter
            {
                Getter     = getter,
                Setter     = setter,
                Type       = propertyInfo.PropertyType,
                ColumnName =
                    options.CustomHeaderNames != null && options.CustomHeaderNames.ContainsKey(propertyInfo.Name)
                        ? options.CustomHeaderNames[propertyInfo.Name]
                        : propertyInfo.Name.ToLowerInvariant(),
                PropertyName          = propertyInfo.Name,
                PropertyMapping       = GetPropertyMapping(options, propertyInfo.Name, propertyInfo.PropertyType),
                RelaxedNumberMatching = options.RelaxedNumberMatching && propertyInfo.PropertyType.IsNumeric()
            };

            if (propertyInfo.Name == options.RowNumberProperty)
            {
                fromCellSetter.ColumnName = null;
            }

            if (options.NotNullProperties.Contains(propertyInfo.Name))
            {
                fromCellSetter.Validators = new List <ExcelCellValidator> {
                    ExcelCellValidatorFactory.CreateRequired()
                };
            }

            return(new[] { fromCellSetter });
        }
예제 #2
0
        private IEnumerable <PropertySetter> GetSettersForEnumerable <T>(PropertyInfo propertyInfo, int index,
                                                                         IExcelToEnumerableOptions <T> options)
        {
            var collectionsConfig = options.CollectionConfigurations[propertyInfo.Name];
            var isDictionary      = typeof(IDictionary).IsAssignableFrom(propertyInfo.PropertyType);
            var enumerableType    = propertyInfo.PropertyType.GenericTypeArguments[0];
            var fromCellSetters   = collectionsConfig.ColumnNames.Select(x => new PropertySetter
            {
                ColumnName   = x.ToLowerInvariant(),
                PropertyName = propertyInfo.Name,
                Setter       = isDictionary
                    ? GetterSetterHelpers.GetDictionaryAdder(propertyInfo, x)
                    : GetterSetterHelpers.GetAdder(propertyInfo),
                Type            = enumerableType,
                PropertyMapping = GetPropertyMapping(options, propertyInfo.Name, enumerableType)
            });

            return(fromCellSetters);
        }