Ejemplo n.º 1
0
        public static T[] ReadFile <T>(string path, CsvConfig config) where T : new()
        {
            if (path.GetIsOleDb())
            {
                return(ReadFile <T>(path, GetTableName <T>(), config));
            }

            if (config == null)
            {
                config = new CsvConfig();

                //object[] attrs = typeof(T).GetCustomAttributes(typeof(CsvObjectAttribute), false);
                if (typeof(T).IsDefined(typeof(CsvObjectAttribute), false))
                {
                    CsvObjectAttribute attr = (CsvObjectAttribute)(typeof(T).GetCustomAttributes(typeof(CsvObjectAttribute), false)[0]);
                    config.CodePage    = attr.CodePage;
                    config.HasHeader   = attr.HasHeader;
                    config.Delimiter   = attr.Delimiter;
                    config.MappingType = attr.MappingType;
                }
            }

            Encoding encoding = Encoding.Default;

            if (config.CodePage != 0)
            {
                encoding = Encoding.GetEncoding(config.CodePage);
            }

            using (StreamReader sr = new StreamReader(path, encoding))
            {
                return(ReadContext <T>(sr.ReadToEnd(), config));
            }
        }
Ejemplo n.º 2
0
        public CsvConfig(Type csvType)
            : this()
        {
            CsvObjectAttribute objAttr = csvType.GetAttribute <CsvObjectAttribute>();

            if (objAttr != null)
            {
                this.CodePage    = objAttr.CodePage;
                this.HasHeader   = objAttr.HasHeader;
                this.MappingType = objAttr.MappingType;
                this.Delimiter   = objAttr.Delimiter;
                this.Name        = objAttr.Name;
                this.ObjectType  = csvType.Name + ", " + csvType.Assembly.GetName().Name;
            }
            else
            {
                this.Name = csvType.Name;
            }

            this.IsChanged = false;

            this.Columns.Clear();
            foreach (PropertyInfo pi in csvType.GetProperties())
            {
                SystemCsvColumnAttribute colAttr = pi.GetAttribute <SystemCsvColumnAttribute>();
                if (colAttr != null)
                {
                    this.Columns.Add(colAttr.Column);
                }
            }
        }