示例#1
0
        void ReadItem(Table table, int row, ObjProxy objProxy)
        {
            int nrows     = table.nrows;
            int ncols     = table.ncols;
            int offset    = 0;
            int headCount = 0;

            for (int r = row; r < nrows; r++)
            {
                if (!string.IsNullOrEmpty(table[r, 0]))
                {
                    headCount += 1;
                }

                if (headCount > 1)
                {
                    break;
                }

                for (int c = 0; c < ncols; c++)
                {
                    var key   = table[0, c];
                    var value = table[r, c];
                    if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value))
                    {
                        continue;
                    }

                    objProxy.SetField(key, value, offset);
                }
                offset++;
            }
        }
示例#2
0
        object BinarySerialize(Table table, Type classtype)
        {
            int nrows      = table.nrows;
            int ncols      = table.ncols;
            var xlss       = classtype.GetAttribute <XLSSAttribute>();
            var fieldNames = new List <string>();

            for (int i = 0; i < ncols; i++)
            {
                if (string.IsNullOrEmpty(table[0, i]))
                {
                    continue;
                }
                fieldNames.Add(table[0, i]);
            }

            var objProxy = new ObjProxy(classtype, fieldNames);

            if (xlss.type == XLSSType.List)
            {
                var fullname = string.Format("System.Collections.Generic.List`1[[{0}]]", classtype.AssemblyQualifiedName);
                var listtype = Type.GetType(fullname);
                var list     = Activator.CreateInstance(listtype) as IList;

                for (int row = 1; row < nrows; row++)
                {
                    var key = table[row, 0];
                    if (!string.IsNullOrEmpty(key))
                    {
                        list.Add(objProxy.New());
                        objProxy.SetContext(table, row);
                        ReadItem(table, row, objProxy);
                    }
                }
                return(list);
            }
            else if (xlss.type == XLSSType.Map)
            {
                Type   keyCls         = null;
                Type   rootCls        = classtype;
                var    primaryKeyAttr = rootCls.GetAttribute <PrimaryKeyAttribute>();
                string keyName        = null;

                if (primaryKeyAttr != null)
                {
                    var keyField = rootCls.GetFields().ToList().Find(x => x.Name == primaryKeyAttr.Value);
                    if (keyField != null)
                    {
                        keyCls  = keyField.FieldType;
                        keyName = keyField.Name;
                    }
                }
                else
                {
                    var keyField = rootCls.GetFields().ToList().Find(x => x.IsPublic);
                    if (keyField != null)
                    {
                        keyCls  = keyField.FieldType;
                        keyName = keyField.Name;
                    }
                }

                int keyCol = 0;
                for (var c = 0; c < ncols; c++)
                {
                    if (table[0, c] == keyName)
                    {
                        keyCol = c;
                    }
                }

                if (keyCls == null)
                {
                    throw new Exception(string.Format("Key of map is not found. table:{0}", table.name));
                }

                var  fullname = string.Format("System.Collections.Generic.Map`2[[{0}],[{1}]]", keyCls.AssemblyQualifiedName, classtype.AssemblyQualifiedName);
                var  maptype  = Type.GetType(fullname);
                IMap map      = Activator.CreateInstance(maptype) as IMap;

                for (int row = 1; row < nrows; row++)
                {
                    var headColValue = table[row, 0];
                    if (!string.IsNullOrEmpty(headColValue))
                    {
                        if (string.IsNullOrEmpty(table[row, keyCol]))
                        {
                            throw new Exception(string.Format("Null string can not be used as a dictionary key. table:{0} row:{1} col:{2}", table.name, row, keyCol));
                        }

                        map.Add(keyCls.Parse(table[row, keyCol]), objProxy.New());
                        objProxy.SetContext(table, row);
                        ReadItem(table, row, objProxy);
                    }
                }
                return(map);
            }
            else if (xlss.type == XLSSType.Obj)
            {
                objProxy.New();
                for (int r = 1; r < nrows; r++)
                {
                    for (int c = 0; c < 2; c++)
                    {
                        var key   = table[r, 0];
                        var value = table[r, 1];
                        objProxy.SetField(key, value, r);
                    }
                }
                return(objProxy.obj);
            }
            return(null);
        }