Ejemplo n.º 1
0
        private List <String> pCsvSaveGetHeader(object e)
        {
            List <String>        cvsHeader = new List <string>();
            Type                 t         = e.GetType();
            CSVClassMapAttribute res       = null;

            PropertyInfo[] p = t.GetProperties();

            for (int i = 0; i < p.Length; i++)
            {
                if (pCvsCheckProperty(p[i], (e as CSVPropertyMapMethod), ref res))
                {
                    continue;
                }

                if (p[i].GetIndexParameters().Length > 0)
                {
                    cvsHeader.Add(t.Name);
                    break;
                }
                else
                {
                    cvsHeader.Add(p[i].Name);
                }
            }
            return(cvsHeader);
        }
Ejemplo n.º 2
0
        private Object pCvsSelectKey(object obj)
        {
            if (obj == null)
            {
                return(null);
            }

            int index = -1;

            PropertyInfo[]       p   = obj.GetType().GetProperties();
            CSVClassMapAttribute res = null;

            for (int i = 0; i < p.Length; i++)
            {
                if (
                    (pCvsCheckProperty(p[i], (obj as CSVPropertyMapMethod), ref res)) ||
                    (res == null) ||
                    (!res.CsvKey)
                    )
                {
                    continue;
                }

                index = ((res.CsvIndex == -1) ? i : res.CsvIndex);
                break;
            }

            index = ((index == -1) ? 0 : index);
            return(p[index].GetValue(obj, null));
        }
Ejemplo n.º 3
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)
         )
         );
 }
Ejemplo n.º 4
0
        public virtual CSVClassMapAttribute FindAttr(string name, bool isNull = false)
        {
            PropertyInfo[] pi = this.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo p in pi)
            {
                CSVClassMapAttribute attr = p.GetCustomAttributes(true)
                                            .Where(c => c.GetType() == typeof(CSVClassMapAttribute))
                                            .Cast <CSVClassMapAttribute>()
                                            .FirstOrDefault();

                if (
                    (attr != null) &&
                    (!string.IsNullOrWhiteSpace(attr.CsvName)) &&
                    (attr.CsvName.Equals(name))
                    )
                {
                    return(new CSVClassMapAttribute(name, attr.CsvKey, attr.CsvIgnore, attr.CsvIndex));
                }
            }
            return((isNull) ?
                   null :
                   new CSVClassMapAttribute(name, false, false, -1)
                   );
        }
Ejemplo n.º 5
0
        private void pCsvSaveGetEle(object e, StringBuilder sb, string fname, string id)
        {
            CSVClassMapAttribute res = null;

            PropertyInfo[] p = e.GetType().GetProperties();

            for (int i = 0; i < p.Length; i++)
            {
                if (pCvsCheckProperty(p[i], (e as CSVPropertyMapMethod), ref res))
                {
                    continue;
                }

                if (i > 0)
                {
                    sb.Append(__escapeChars[0]);
                }

                if (p[i].GetIndexParameters().Length > 0)
                {
                    string raw = e as string;
                    if (!String.IsNullOrWhiteSpace(raw))
                    {
                        sb.Append(raw);
                    }
                    break;
                }

                object obj;

                try
                {
                    if ((obj = p[i].GetValue(e, null)) == null)
                    {
                        continue;
                    }
                }
                catch (Exception)
                {
                    continue;
                }

                if (obj != null)
                {
                    if (obj is IEnumerable <Object> )
                    {
                        try
                        {
                            IEnumerable <Object> ie = (obj as IEnumerable <Object>);
                            if (ie != null)
                            {
                                int cnt;
                                try { cnt = (obj as dynamic).Count; }
                                catch (Exception) { cnt = 1; }

                                if (cnt > 0)
                                {
                                    pCsvSaveIEnumerable(ie, fname, p[i].Name, id);

                                    if (!IsLoadChildren)
                                    {
                                        bool   IsIenumerable = false;
                                        Object x             = pCvsGetIEnumerableEle(p[i], ref IsIenumerable);

                                        if ((IsIenumerable) && (x != null))
                                        {
                                            p[i].SetValue(e, x, null);
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception) { }
                    }
                    else if (obj.GetType() == typeof(byte[]))
                    {
                        if (((byte[])(obj)).Length > 0)
                        {
                            sb.Append(pCsvBytesToString((byte[])obj));
                        }
                    }
                    else
                    {
                        sb.Append(obj);
                    }
                }
            }
        }
Ejemplo n.º 6
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);
        }