Пример #1
0
 private bool CreateOneUserSpecifiedConverterForOneProperty(ColumnToPropertyMap oneMap, ICsvConverter converter,
                                                            CsvConverterAttribute oneAttribute, bool throwExceptionIfConverterHasAlreadyBeenSpecified)
 {
     // Possible pre or post converter.
     if (oneAttribute is CsvConverterStringAttribute stringConverter &&
         (stringConverter.IsPreConverter || stringConverter.IsPostConverter))
     {
         AddOnePropertyPreOrPostConverter(oneMap, converter, stringConverter);
         return(false);
     }
Пример #2
0
        public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
        {
            var myAttribute = attribute as CsvConverterTextLengthEnforcerAttribute;

            if (myAttribute == null)
            {
                throw new ArgumentException($"Please use the {nameof(CsvConverterTextLengthEnforcerAttribute)} attribute with this pre-converter!");
            }

            _maximumLength = myAttribute.MaximumLength;
            _minimumLength = myAttribute.MinimumLength;
            _characterToAddToShortStrings = myAttribute.CharacterToAddToShortStrings;
        }
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterStringAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"All string converters should be used with attributes that derive from the " +
                          $"{nameof(CsvConverterStringAttribute)}!");
            }

            Order = oneAttribute.Order;
        }
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);
            if (!(attribute is CsvConverterNumberAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterNumberAttribute)} " +
                          $"attribute with the {nameof(CsvConverterMoney)} converter.");
            }

            CreateConverter(typeof(decimal), oneAttribute, defaultFactory);
            CreateConverter(typeof(decimal?), oneAttribute, defaultFactory);
            CreateConverter(typeof(double), oneAttribute, defaultFactory);
            CreateConverter(typeof(double?), oneAttribute, defaultFactory);
        }
Пример #5
0
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterNumericRangeAttribute oneAttribute))
            {
                throw new ArgumentException($"Please use the {nameof(CsvConverterNumericRangeAttribute)} attribute with this converter!");
            }

            _intConverter = defaultFactory.CreateConverter(typeof(int));
            _intConverter.Initialize(oneAttribute, defaultFactory);

            _minimum = oneAttribute.Minimum;
            _maximum = oneAttribute.Maximum;
        }
Пример #6
0
        public MapModel(int fieldIndex, string fieldName, PropertyInfo propertyInfo, CsvConverterAttribute converterAttribute)
        {
            FieldIndex   = fieldIndex;
            PropertyInfo = propertyInfo;
            FieldName    = fieldName;

            if (converterAttribute != null)
            {
                ValueConverter = Activator.CreateInstance(converterAttribute.ConverterType, converterAttribute.Params) as ICsvValueConverter;
                if (ValueConverter == null)
                {
                    throw new InvalidConverterType($"Could not create instance of {converterAttribute.ConverterType.Name}. Make sure it is of type {typeof(ICsvValueConverter).Name}");
                }
            }
            else
            {
                Dictionary <Type, ICsvValueConverter> defaultConverters = DefaultConvertersFactory.Create();
                if (defaultConverters.ContainsKey(PropertyInfo.PropertyType))
                {
                    ValueConverter = defaultConverters[PropertyInfo.PropertyType];
                }
            }
        }
Пример #7
0
        private void FormNameDictionary(Type type)
        {
            try
            {
                colNamesDictionary = new Dictionary <string, string>();

                PropertyInfo[] propertiesInfo = type.GetProperties();
                foreach (PropertyInfo info in propertiesInfo)
                {
                    CsvConverterAttribute attribute = info.GetCustomAttribute(typeof(CsvConverterAttribute)) as CsvConverterAttribute;
                    if (attribute != null)
                    {
                        if (attribute.Ignore)
                        {
                            continue;
                        }

                        if (!String.IsNullOrEmpty(attribute.Name))
                        {
                            colNamesDictionary.Add(attribute.Name, info.Name);
                            continue;
                        }
                    }
                    colNamesDictionary.Add(info.Name, info.Name);
                }

                if (colNamesDictionary.Count == 0)
                {
                    throw new CsvConverterException($"Error: HeadingRow doesn't contain similar values to the properties of the class {type.Name}.");
                }
            }
            catch (Exception e)
            {
                throw new CsvConverterException("Method: FormNameDictionary.", e);
            }
        }