Example #1
0
        /// <summary>
        /// Loads quantities from the specified <see cref="StreamReader"/>.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the output items.
        /// </typeparam>
        /// <param name="r">
        /// The input <see cref="StreamReader"/>.
        /// </param>
        /// <param name="cultureInfo">
        /// The culture.
        /// </param>
        /// <returns>
        /// A list of items.
        /// </returns>
        /// <exception cref="System.FormatException">
        /// Unit not recognized
        /// </exception>
        public static IList <T> LoadQuantities <T>(StreamReader r, CultureInfo cultureInfo = null) where T : IQuantity
        {
            if (cultureInfo == null)
            {
                cultureInfo = CultureInfo.InvariantCulture;
            }

            var type = typeof(T);

            // Get the properties from the type
            var header = r.ReadLine();

            if (header == null)
            {
                return(null);
            }

            // Parse the header
            string name, unit;

            CsvFile.SplitHeader(header, out name, out unit);

            IQuantity displayUnit;

            if (UnitProvider.Default.TryGetUnit(type, unit, out displayUnit))
            {
            }
            else
            {
                throw new FormatException("Unit " + unit + " not recognized");
            }

            // Read the rows
            var items = new List <T>();

            while (!r.EndOfStream)
            {
                var line = r.ReadLine();
                if (line == null)
                {
                    continue;
                }

                var value = ChangeType(line, type, displayUnit, cultureInfo);
                items.Add((T)value);
            }

            return(items);
        }
Example #2
0
        /// <summary>
        /// Loads items from the specified <see cref="StreamReader"/>.
        /// </summary>
        /// <typeparam name="T">
        /// The type of the output items.
        /// </typeparam>
        /// <param name="r">
        /// The input <see cref="StreamReader"/>.
        /// </param>
        /// <param name="cultureInfo">
        /// The input culture.
        /// </param>
        /// <returns>
        /// A list of items.
        /// </returns>
        /// <exception cref="System.FormatException">
        /// Unit not recognized
        /// </exception>
        public static IList <T> Load <T>(StreamReader r, CultureInfo cultureInfo = null)
        {
            if (cultureInfo == null)
            {
                cultureInfo = CultureInfo.InvariantCulture;
            }

            var separator = cultureInfo.TextInfo.ListSeparator;
            var type      = typeof(T);

            // Get the properties from the type
            var properties = type.GetTypeInfo().DeclaredProperties.ToArray();
            var header     = r.ReadLine();

            if (header == null)
            {
                return(null);
            }

            // Parse the header
            var headers             = CsvFile.SplitLine(header, separator[0]);
            int n                   = headers.Length;
            var propertyDescriptors = new PropertyInfo[n];
            var units               = new IQuantity[n];

            for (int i = 0; i < n; i++)
            {
                string name, unit;
                CsvFile.SplitHeader(headers[i], out name, out unit);
                propertyDescriptors[i] = properties.First(pi => pi.Name == name);
                var quantityType = CsvFile.GetQuantityType(propertyDescriptors[i].PropertyType);

                // Set the unit if it is a IQuantity based property
                if (quantityType != null)
                {
                    IQuantity displayUnit;
                    if (UnitProvider.Default.TryGetUnit(quantityType, unit, out displayUnit))
                    {
                        units[i] = displayUnit;
                    }
                    else
                    {
                        throw new FormatException("Unit " + unit + " not recognized");
                    }
                }
            }

            // Read the rows
            var items      = new List <T>();
            int lineNumber = 1;

            while (!r.EndOfStream)
            {
                lineNumber++;
                var line = r.ReadLine();
                if (line == null)
                {
                    continue;
                }

                var values = CsvFile.SplitLine(line, separator[0]);
                if (values.Length != n)
                {
                    throw new FormatException("Wrong number of columns on line " + lineNumber);
                }

                var item = Activator.CreateInstance <T>();
                for (int i = 0; i < headers.Length; i++)
                {
                    var value = ChangeType(values[i], propertyDescriptors[i].PropertyType, units[i], cultureInfo);
                    propertyDescriptors[i].SetValue(item, value, null);
                }

                items.Add(item);
            }

            return(items);
        }