示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FileColumnReflector"/> class.
        /// </summary>
        /// <param name="index">The <see cref="Index"/>.</param>
        /// <param name="fc">The <see cref="FileColumn"/>.</param>
        /// <param name="pi">The <see cref="PropertyInfo"/>.</param>
        /// <param name="ff">The <see cref="FileFormat"/>.</param>
        internal FileColumnReflector(int index, FileColumnAttribute fc, PropertyInfo pi, FileFormatBase ff)
        {
            Index        = index;
            Order        = fc.Order < 0 ? int.MaxValue : fc.Order;
            Name         = string.IsNullOrEmpty(fc.Name) ? pi.Name : fc.Name;
            FileColumn   = fc;
            PropertyInfo = pi;
            FileFormat   = ff;

            // Get the property type being mindful of nullables.
            if (PropertyInfo.PropertyType.GetTypeInfo().IsGenericType&& PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                PropertyTypeCode   = GetTypeCode(PropertyInfo.PropertyType.GetGenericArguments()[0]);
                PropertyIsNullable = true;
            }
            else
            {
                PropertyTypeCode = GetTypeCode(PropertyInfo.PropertyType);
            }

            _valueConverter = FileFormat.Converters.Get(PropertyInfo.PropertyType, FileColumn.TextValueConverterKey, FileColumn.TextValueConverterType);
            if (!string.IsNullOrEmpty(FileColumn.TextValueConverterKey) && _valueConverter == null)
            {
                throw new InvalidOperationException(string.Format("FileColumnAttribute has TextValueConverterKey of '{0}' is not found within the FileFormat.Converters.", FileColumn.TextValueConverterKey));
            }

            if (FileColumn.IsLineNumber && PropertyTypeCode != TypeCode.Int32 && PropertyTypeCode != TypeCode.Int64)
            {
                throw new InvalidOperationException("FileColumnAttribute has IsLineNumber set to true; the underlying property type must be either Int32 or Int64.");
            }
        }
示例#2
0
        /// <summary>
        /// Adds a default <see cref="ITextValueConverter{T}"/> for the <see cref="Type"/>.
        /// </summary>
        /// <typeparam name="T">The underlying <see cref="Type"/> being converted.</typeparam>
        /// <param name="converter">The <see cref="ITextValueConverter{T}"/>.</param>
        public void Add <T>(ITextValueConverter <T> converter)
        {
            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            _dict.Add(GetKey(null, typeof(T)), new TextValueConverter {
                Type = typeof(T), Converter = converter
            });
        }
示例#3
0
        /// <summary>
        /// Adds an <see cref="ITextValueConverter{T}"/> with a corresponding <paramref name="key"/>.
        /// </summary>
        /// <typeparam name="T">The underlying <see cref="Type"/> being converted.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="converter">The <see cref="ITextValueConverter{T}"/>.</param>
        public void Add <T>(string key, ITextValueConverter <T> converter)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (converter == null)
            {
                throw new ArgumentNullException(nameof(converter));
            }

            _dict.Add(GetKey(key, typeof(T)), new TextValueConverter {
                Type = typeof(T), Converter = converter
            });
        }