Example #1
0
        public IList ReadList <T>(ref T l)
        {
            int count = 0;

            Read(ref count);

            IList list = l as IList;            //BasicClassTypeUtil.CreateObject(l.GetType()) as IList;

            if (list == null)
            {
                TLog.TError("ReadList list == null");
                return(null);
            }

            list.Clear();


            for (int i = 0; i < count; i++)
            {
                object objItem = BasicClassTypeUtil.CreateListItem(list.GetType());
                Read(ref objItem);
                list.Add(objItem);
            }

            return(list);
        }
Example #2
0
        public IDictionary ReadMap <T>(ref T map)
        {
            IDictionary m = BasicClassTypeUtil.CreateObject(map.GetType()) as IDictionary;

            if (m == null)
            {
                return(null);
            }

            m.Clear();

            int count = 0;

            Read(ref count);
            if (count > 0)
            {
                Type   type     = m.GetType();
                Type[] argsType = type.GetGenericArguments();
                if (argsType == null || argsType.Length < 2)
                {
                    return(null);
                }

                for (int i = 0; i < count; i++)
                {
                    var mk = BasicClassTypeUtil.CreateObject(argsType[0]);
                    var mv = BasicClassTypeUtil.CreateObject(argsType[1]);

                    mk = Read(ref mk);
                    mv = Read(ref mv);

                    m.Add(mk, mv);
                }
                map = (T)m;
                return(m);
            }
            return(null);
        }
Example #3
0
        public object Read <T>(ref T o)
        {
            if (o == null)
            {
                o = (T)BasicClassTypeUtil.CreateObject <T>();
            }

            if (o is Byte || o is Char)
            {
                byte b = 0;

                o = (T)(object)(Read(ref b));
            }
            else if (o is char)
            {
                byte c = 0;
                o = (T)(object)(Read(ref c));
            }
            else if (o is Boolean)
            {
                bool b = false;
                o = (T)(object)(Read(ref b));
            }
            else if (o is short)
            {
                short s = 0;
                o = (T)(object)(Read(ref s));
            }
            else if (o is ushort)
            {
                ushort us = 0;
                o = (T)(object)(Read(ref us));
            }
            else if (o is int)
            {
                int i = 0;
                o = (T)(object)Read(ref i);
            }
            else if (o is uint)
            {
                uint ui = 0;
                o = (T)(object)Read(ref ui);
                return(o);
            }
            else if (o is long)
            {
                long l = 0;
                o = (T)(object)Read(ref l);
                return(o);
            }
            else if (o is Enum)
            {
                int remp = 0;
                o = (T)(object)Read(ref remp);
                return(o);
            }
            else if (o is ulong)
            {
                ulong  ul = 0;
                object oo = (Read(ref ul));
                o = (T)oo;
                return(oo);
            }            /*
                          * else if (o is float)
                          * {
                          * return (Read());
                          * }
                          * else if (o is Double)
                          * {
                          * return (Read());
                          * }*/
            else if (o is string)
            {
                string s = "";
                o = (T)(object)Read(ref s);
            }
            else if (o is TBufferBase)
            {
                TBufferBase oo = o as TBufferBase;
                o = (T)(object)Read(ref oo);
            }
            else if (o != null && o.GetType().IsArray)
            {
                /*
                 * if (o is byte[] || o is Byte[])
                 * {
                 * return Read((byte[])null);
                 * }
                 * else if (o is bool[])
                 * {
                 * return Read((bool[])null);
                 * }
                 * else if (o is short[])
                 * {
                 * return Read((short[])null);
                 * }
                 * else if (o is int[])
                 * {
                 * return Read((int[])null);
                 * }
                 * else if (o is long[])
                 * {
                 * return Read((long[])null);
                 * }
                 * else if (o is float[])
                 * {
                 * return Read((float[])null);
                 * }
                 * else if (o is double[])
                 * {
                 * return Read((double[])null);
                 * }
                 * else
                 * {
                 * object oo = o;
                 * return readArray((Object[])oo);
                 * }
                 */
            }
            else if (o is IList)
            {
                return(ReadList <T>(ref o));
            }
            else if (o is IDictionary)
            {
                return(ReadMap <T>(ref o));
            }
            else
            {
                throw new Exception("read object error: unsupport type:" + o.GetType() + " value:" + o.ToString());
            }

            return(o);
        }