Exemplo n.º 1
0
        // --------------------------------------------------------------------------------------------------------------------------
        /// <summary>
        /// Add a line to the file, using reflection to read source data from the given type.
        /// </summary>
        public void AddLineFromData <T>(T data)
        {
            Type t = typeof(T);
            List <CSVColToPropertyMap> propList = CSVLine.GetPropList(t, ColumnMap);

            string[] vals = new string[ColumnMap.Count];
            foreach (var p in propList)
            {
                int index = ColumnMap.GetIndex(p.ColName);
                if (index != -1)
                {
                    object val = p.PropInfo.GetValue(data, null);

                    string useVal = val?.ToString() ?? ""; // FixCSVData(val?.ToString() ?? "");

                    if (p.PropInfo.PropertyType == typeof(string))
                    {
                        // Just quote all string fields...
                        // Also make sure to escape any existing quotes.
                        useVal = useVal.Replace("\"", "\"\"");
                        useVal = "\"" + useVal + "\"";
                    }
                    vals[index] = useVal;
                }
            }

            Lines.Add(new CSVLine(ColumnMap, vals));
        }