示例#1
0
        public IExcelPropertyConfiguration <T, TProperty> Property <TProperty>(string propertyName)
        {
            var excelPropertyConfiguration =
                new ExcelPropertyConfiguration <T, TProperty>(this, propertyName, _options);

            return(excelPropertyConfiguration);
        }
        public IExcelPropertyConfiguration <T, TProperty> Property <TProperty>(
            Expression <Func <T, TProperty> > propertyExpression)
        {
            var excelPropertyConfiguration =
                new ExcelPropertyConfiguration <T, TProperty>(this, GetExpressionName(propertyExpression), _options);

            return(excelPropertyConfiguration);
        }
        internal static void AddOptionsFromAttributes(ExcelToEnumerableOptionsBuilder <T> builder, Type type)
        {
            MapClassLevelAttributes(builder, type);

            var properties = type.GetProperties().Distinct().ToArray();

            foreach (var property in properties)
            {
                var propertyAttributes = property.CustomAttributes.ToArray();
                foreach (var propertyAttribute in propertyAttributes)
                {
                    switch (propertyAttribute.AttributeType.Name)
                    {
                    case nameof(MapsToColumnNumberAttribute):
                        var columnNumber = (int)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNumber(columnNumber, property.Name, builder._options);
                        break;

                    case nameof(MapsToColumnLetterAttribute):
                        var columnLetter = (string)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNumber(CellRef.ColumnNameToNumber(columnLetter),
                                                                      property.Name, builder._options);
                        break;

                    case nameof(OptionalColumnAttribute):
                        ExcelPropertyConfiguration.OptionalColumn(true, property.Name, builder._options);
                        break;

                    case nameof(MapFromColumnsAttribute):
                        var columnNames =
                            ((ReadOnlyCollection <CustomAttributeTypedArgument>)propertyAttribute
                             .ConstructorArguments[0].Value).Select(x => x.Value.ToString());
                        ExcelPropertyConfiguration.MapFromColumns(columnNames, property.Name, builder._options);
                        break;

                    case nameof(MapsToColumnNamedAttribute):
                        var columnName = (string)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.MapsToColumnNamed(columnName, property.Name, builder._options);
                        break;

                    case nameof(IgnoreColumnAttribute):
                        ExcelPropertyConfiguration.Ignore(property.Name, builder._options);
                        break;

                    case nameof(MapsToRowNumberAttribute):
                        ExcelPropertyConfiguration.MapsToRowNumber(property.Name, builder._options);
                        break;

                    case nameof(ShouldBeLessThanAttribute):
                        var maxValue = (double)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.ShouldBeLessThan(maxValue, property.Name, builder._options);
                        break;

                    case nameof(ShouldBeGreaterThanAttribute):
                        var minValue = (double)propertyAttribute.ConstructorArguments[0].Value;
                        ExcelPropertyConfiguration.ShouldBeGreaterThan(minValue, property.Name, builder._options);
                        break;

                    case nameof(NotNullAttribute):
                        ExcelPropertyConfiguration.NotNullProperties(property.Name, builder._options);
                        break;

                    case nameof(ShouldBeOneOfAttribute):
                        // CSH 27112020 We're adding a validator directly here, rather than going via the static ExcelPropertyConfiguration because the validator we're using here using
                        // an enumerable of type object rather than type TProperty (since enforcing argument types at compile time is not possible with Attributes)
                        var objectList =
                            ((ReadOnlyCollection <CustomAttributeTypedArgument>)propertyAttribute
                             .ConstructorArguments[0].Value).Select(x => x.Value);
                        builder._options.Validations[property.Name]
                        .Add(ExcelCellValidatorFactory.CreateShouldBeOneOf(objectList));
                        break;

                    case nameof(UniqueAttribute):
                        ExcelPropertyConfiguration.Unique(property.Name, builder._options);
                        break;

                    case nameof(RequiredColumnAttribute):
                        ExcelPropertyConfiguration.RequiredColumn(true, property.Name, builder._options);
                        break;
                    }
                }
            }
        }