Exemple #1
0
 private bool pCvsCheckProperty(PropertyInfo p, CSVPropertyMapMethod mmt, ref CSVClassMapAttribute res)
 {
     return(
         (p == null) ||
         (!p.CanRead) ||
         (
             (mmt != null) &&
             ((res = mmt.FindAttr(p.Name, true)) != null) &&
             (res.CsvIgnore)
         )
         );
 }
Exemple #2
0
        private uint pCsvLoad(Type t, string fname, string elename, string id)
        {
            int  nele;
            uint cnt = 0;

            PropertyInfo[] p;
            string         _fname = pCsvGetChildFileName(fname, elename);

            if (!File.Exists(_fname))
            {
                return(cnt);
            }

            if (Count != 0)
            {
                Clear();
            }

            if ((p = t.GetProperties()) == null)
            {
                return(0);
            }

            nele = p.Length;

            using (StreamReader rd = new StreamReader(_fname, EncodingFile))
            {
                string line;
                Object Id  = null,
                       obj = Activator.CreateInstance(t);
                CSVClassMapAttribute res = null;
                CSVPropertyMapMethod mmt = obj as CSVPropertyMapMethod;

                while ((line = rd.ReadLine()) != null)
                {
                    cnt++;
                    Id = null;

                    if (
                        (LineSkip > cnt) ||
                        ((cnt == 1) && (IsHeader)) ||
                        (String.IsNullOrWhiteSpace(line))
                        )
                    {
                        continue;
                    }

                    int      cells = 0;
                    string[] part  = line.Split(new char[] { __escapeChars[0] }, nele, StringSplitOptions.None);

                    if ((IsStrict) && (part.Length != nele))
                    {
                        for (int i = 0; i < nele; i++)
                        {
                            if (pCvsCheckProperty(p[i], mmt, ref res))
                            {
                                continue;
                            }
                            cells++;
                        }
                        if (cells == 0)
                        {
                            continue;
                        }

                        nele  = cells;
                        cells = 0;
                    }

                    for (int i = 0, n = 0; ((i < part.Length) && (cells < nele)); i++, n++)
                    {
                        try
                        {
                            string str;
                            res = null;

                            if (n >= p.Length)
                            {
                                break;
                            }

                            if (pCvsCheckProperty(p[n], mmt, ref res))
                            {
                                --i;
                                continue;
                            }

                            cells++;

                            try
                            {
                                /// Type List<>, IEnumerable<>
                                if (p[n].PropertyType.IsGenericType)
                                {
                                    bool   IsIenumerable = false;
                                    Object ie            = pCvsGetIEnumerableEle(p[n], ref IsIenumerable);

                                    if (IsIenumerable)
                                    {
                                        if (ie == null)
                                        {
                                            continue;
                                        }

                                        p[n].SetValue(obj, ie, null);

                                        if ((res != null) && (res.CsvKey))
                                        {
                                            Id = p[n].GetValue(obj, null);
                                        }

                                        continue; // TODO: load data
                                    }
                                }
                            }
                            catch (Exception)
                            {
                                continue;
                            }

                            if (
                                (res != null) &&
                                (res.CsvIndex > -1) &&
                                (res.CsvIndex < part.Length)
                                )
                            {
                                str = part[res.CsvIndex];
                            }
                            else
                            {
                                str = part[i];
                            }

                            if (String.IsNullOrWhiteSpace(str))
                            {
                                if (i == 0)
                                {
                                    break;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                            if (IsTrim)
                            {
                                str.Trim();
                            }
                            if (str.StartsWith("\"") && str.EndsWith("\""))
                            {
                                str = str.Substring(1, str.Length - 2).Replace("\"\"", "\"");
                            }

                            if (p[n].PropertyType == typeof(byte[]))
                            {
                                p[n].SetValue(obj, pCsvStringToBytes(str), null);
                            }
                            else
                            {
                                p[n].SetValue(obj, Convert.ChangeType(str, p[n].PropertyType), null);
                            }


                            if (
                                (n == 0) ||
                                ((res != null) && (res.CsvKey))
                                )
                            {
                                Id = p[n].GetValue(obj, null);
                            }
                        }
                        catch (Exception)
                        {
                            Id = null;
                        }
                    }

                    if (Id != null)
                    {
                        Add((Tkey)Id, obj);
                    }
                }
            }
            return((IsHeader) ? ((cnt > 0) ? (cnt - 1) : 0) : cnt);
        }