Beispiel #1
0
        private static IExcelToEnumerableOptions <T> BuildOptions <T>(Action <ExcelToEnumerableOptionsBuilder <T> > options)
        {
            var optionsBuilder = new ExcelToEnumerableOptionsBuilder <T>();

            options?.Invoke(optionsBuilder);
            return(optionsBuilder.Build());
        }
        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;
                    }
                }
            }
        }
        private static void MapClassLevelAttributes(ExcelToEnumerableOptionsBuilder <T> builder,
                                                    Type type)
        {
            var attributes = type.CustomAttributes;

            foreach (var attribute in attributes)
            {
                switch (attribute.AttributeType.Name)
                {
                case nameof(AllPropertiesMustBeMappedToColumnsAttribute):
                    builder.AllPropertiesMustBeMappedToColumns((bool)attribute.ConstructorArguments[0].Value);
                    break;

                case nameof(AllColumnsMustBeMappedToPropertiesAttribute):
                    builder.AllColumnsMustBeMappedToProperties((bool)attribute.ConstructorArguments[0].Value);
                    break;

                case nameof(UsingHeaderNamesAttribute):
                    builder.UsingHeaderNames((bool)attribute.ConstructorArguments[0].Value);
                    break;

                case nameof(StartingFromRowAttribute):
                    var rowNumber = (int)attribute.ConstructorArguments[0].Value;
                    builder.StartingFromRow(rowNumber);
                    break;

                case nameof(UsingSheetAttribute):
                    var sheetArgument = attribute.ConstructorArguments[0].Value;
                    if (sheetArgument is int argument)
                    {
                        builder.UsingSheet(argument);
                    }
                    else
                    {
                        builder.UsingSheet(sheetArgument.ToString());
                    }

                    break;

                case nameof(HeaderOnRowAttribute):
                    var headerOnRowNumber = (int)attribute.ConstructorArguments[0].Value;
                    builder.HeaderOnRow(headerOnRowNumber);
                    break;

                case nameof(EndingWithRowAttribute):
                    builder.EndingWithRow((int)attribute.ConstructorArguments[0].Value);
                    break;

                case nameof(AggregateExceptionsAttribute):
                    builder.AggregateExceptions();
                    break;

                case nameof(BlankRowBehaviourAttribute):
                    builder.BlankRowBehaviour((BlankRowBehaviour)attribute.ConstructorArguments[0].Value);
                    break;

                case nameof(RelaxedNumberMatchingAttribute):
                    builder.RelaxedNumberMatching((bool)attribute.ConstructorArguments[0].Value);
                    break;
                }
            }
        }