Beispiel #1
0
        private MappingConfig CreateMappingConfigForType(Type type, SheetToObjectAttributeConfig sheetToAttributeConfigAttribute)
        {
            var mappingConfig = new MappingConfig
            {
                HasHeaders        = sheetToAttributeConfigAttribute.SheetHasHeaders,
                AutoMapProperties = sheetToAttributeConfigAttribute.AutoMapProperties
            };

            foreach (var property in type.GetProperties())
            {
                var columnIsMappedByAttribute = false;
                var mappingConfigBuilder      = new ColumnMappingBuilder <T>();
                var attributes = property.GetCustomAttributes().ToList();

                if (attributes.OfType <IgnorePropertyMapping>().Any())
                {
                    continue;
                }

                foreach (var mappingAttribute in attributes.OfType <IMappingAttribute>())
                {
                    mappingAttribute.SetColumnMapping(mappingConfigBuilder);
                    columnIsMappedByAttribute = true;
                }

                foreach (var attribute in attributes)
                {
                    switch (true)
                    {
                    case var _ when attribute is IParsingRuleAttribute:
                        mappingConfigBuilder.AddParsingRule(((IParsingRuleAttribute)attribute).GetRule());
                        break;

                    case var _ when attribute is IRuleAttribute:
                        mappingConfigBuilder.AddRule(((IRuleAttribute)attribute).GetRule());
                        break;

                    case var _ when attribute is IColumnRuleAttribute:
                        mappingConfigBuilder.AddRule(((IColumnRuleAttribute)attribute).GetRule());
                        break;

                    case var _ when attribute is Format:
                        mappingConfigBuilder.UsingFormat(((Format)attribute).FormatString);
                        break;

                    case var _ when attribute is DefaultValue:
                        mappingConfigBuilder.WithDefaultValue(((DefaultValue)attribute).Value);
                        break;

                    case var _ when attribute is RequiredInHeaderRow:
                        mappingConfigBuilder.WithRequiredInHeaderRow();
                        break;
                    }
                }

                if (columnIsMappedByAttribute || mappingConfig.AutoMapProperties)
                {
                    mappingConfig.ColumnMappings.Add(mappingConfigBuilder.MapTo(property));
                }
            }

            return(mappingConfig);
        }
 /// <summary>
 /// Values in this column have to be unique
 /// </summary>
 public static ColumnMappingBuilder <T> ShouldHaveUniqueValues <T>(this ColumnMappingBuilder <T> columnMappingBuilder)
 {
     columnMappingBuilder.AddRule(new UniqueValuesInColumnRule());
     return(columnMappingBuilder);
 }
 /// <summary>
 /// Values in this column need to match the given regex
 /// </summary>
 public static ColumnMappingBuilder <T> Matches <T>(this ColumnMappingBuilder <T> columnMappingBuilder, string regex)
 {
     columnMappingBuilder.AddRule(new RegexRule(regex));
     return(columnMappingBuilder);
 }
 /// <summary>
 /// Values in this column can have a maximum of the given value
 /// </summary>
 public static ColumnMappingBuilder <T> WithMaximum <T, TComparer>(this ColumnMappingBuilder <T> columnMappingBuilder, TComparer comparer)
     where TComparer : IComparable <TComparer>
 {
     columnMappingBuilder.AddRule(new MaximumRule <TComparer>(comparer));
     return(columnMappingBuilder);
 }
 /// <summary>
 /// Make values in this column required, which means it can not be empty
 /// </summary>
 public static ColumnMappingBuilder <T> IsRequired <T>(this ColumnMappingBuilder <T> columnMappingBuilder)
 {
     columnMappingBuilder.AddParsingRule(new RequiredRule());
     return(columnMappingBuilder);
 }