Exemple #1
0
        // ToDo Write tests for this
        public static void SetPropertiesFromData <line_type>(TeaboxDataLine line) where line_type : TeaboxDataLine
        {
            foreach (var prop in typeof(line_type).GetProperties())
            {
                var atts = prop.GetCustomAttributes(typeof(TeaboxDataAttribute), true);

                if (atts.Length == 1 && atts[0].GetType() == typeof(TeaboxDataAttribute))
                {
                    if (prop.PropertyType == typeof(string))
                    {
                        prop.SetValue(line, TeaboxDataLine.GetData(line, prop.Name));
                    }
                    else if (prop.PropertyType == typeof(int))
                    {
                        int v = 0;
                        int.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v);
                        prop.SetValue(line, v);
                    }
                    else if (prop.PropertyType == typeof(DateTime))
                    {
                        DateTime v = DateTime.MinValue;
                        DateTime.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v);
                        prop.SetValue(line, v);
                    }
                    else if (prop.PropertyType == typeof(bool))
                    {
                        bool v = false;
                        bool.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v);
                        prop.SetValue(line, v);
                    }
                    else if (prop.PropertyType == typeof(double))
                    {
                        double v = 0;
                        double.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v);
                        prop.SetValue(line, v);
                    }
                    else if (prop.PropertyType == typeof(decimal)) // ToDo Test
                    {
                        decimal v = 0;
                        decimal.TryParse(TeaboxDataLine.GetData(line, prop.Name), out v);
                        prop.SetValue(line, v);
                    }
                    else
                    {
                        throw new Exception("Property type not supported.");
                    }
                }
            }
        }
        protected void WriteFile(IFileContainer file)
        {
            //if (_titles == null)
            //    _titles = new string[0]; // If set in constructor. Don't overwrite it here.

            var raw_lines = new List <string>();

            foreach (var line in _lines)
            {
                string comment = TeaboxDataLine.GetComment(line);
                var    type    = TeaboxDataLine.GetLineType(line);

                string raw_line = "";

                if (type == TeaboxDataLineType.Titles)
                {
                    raw_line += (TitleRowIdentifier +
                                 string.Join(DataDelimiter, TeaboxDataLine.GetData(line)));
                }
                else if (type == TeaboxDataLineType.Data)
                {
                    TeaboxDataLine.SetDataFromProperties <item_type>(line);

                    string line_data = "";

                    if (_titles == null || _titles.Length == 0)
                    {
                        var parts = TeaboxDataLine.GetData(line);

                        for (int i = 0; i < parts.Count; i++)
                        {
                            if (i > 0)
                            {
                                line_data += DataDelimiter;
                            }
                            line_data += parts[i];
                        }
                    }
                    else
                    {
                        for (int i = 0; i < _titles.Length; i++)
                        {
                            if (i > 0)
                            {
                                line_data += DataDelimiter;
                            }
                            line_data += TeaboxDataLine.GetData(line, _titles[i]);
                        }
                    }


                    raw_line += line_data;
                }

                if (!string.IsNullOrEmpty(comment))
                {
                    if (raw_line != "")
                    {
                        raw_line += " ";
                    }

                    raw_line += (CommentIdentifier + comment);
                }

                raw_lines.Add(raw_line);
            }

            file.WriteAllLines(raw_lines);
        }