Exemplo n.º 1
0
        private static void SerializeSimple(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter2 msWriter)
        {
            //if (bufType.EntityType == EntityType.COMPLEX)
            //{
            //    throw new Exception("无法序列化复杂类型");
            //}

            switch (bufType.EntityType)
            {
            case EntityType.BYTE:
                if (isArray)
                {
                    msWriter.WriteByteArray((byte[])val);
                }
                else
                {
                    msWriter.WriteByte((byte)val);
                }
                break;

            case EntityType.STRING:
                if (isArray)
                {
                    msWriter.WriteStringArray((string[])val);
                }
                else
                {
                    msWriter.WriteString((string)val);
                }
                break;

            case EntityType.SHORT:
            case EntityType.INT16:
                if (isArray)
                {
                    msWriter.WriteInt16Array((Int16[])val);
                }
                else
                {
                    msWriter.WriteInt16((Int16)val);
                }
                break;

            case EntityType.USHORT:
                if (isArray)
                {
                    msWriter.WriteUInt16Array((UInt16[])val);
                }
                else
                {
                    msWriter.WriteUInt16((UInt16)val);
                }
                break;

            case EntityType.INT32:
                if (isArray)
                {
                    msWriter.WriteInt32Array((Int32[])val);
                }
                else
                {
                    msWriter.WriteInt32((Int32)val);
                }
                break;

            case EntityType.CHAR:
                if (isArray)
                {
                    msWriter.WriteCharArray((char[])val);
                }
                else
                {
                    msWriter.WriteChar((char)val);
                }
                break;

            case EntityType.DECIMAL:
                if (isArray)
                {
                    msWriter.WriteDeciamlArray((decimal[])val);
                }
                else
                {
                    msWriter.WriteDecimal((decimal)val);
                }
                break;

            case EntityType.DOUBLE:
                if (isArray)
                {
                    msWriter.WriteDoubleArray((double[])val);
                }
                else
                {
                    msWriter.WriteDouble((double)val);
                }
                break;

            case EntityType.FLOAT:
                if (isArray)
                {
                    msWriter.WriteFloatArray((float[])val);
                }
                else
                {
                    msWriter.WriteFloat((float)val);
                }
                break;

            case EntityType.INT64:
                if (isArray)
                {
                    msWriter.WriteInt64Array((Int64[])val);
                }
                else
                {
                    msWriter.WriteInt64((Int64)val);
                }
                break;

            case EntityType.DATETIME:
                if (isArray)
                {
                    msWriter.WriteDateTimeArray((DateTime[])val);
                }
                else
                {
                    msWriter.WriteDateTime((DateTime)val);
                }
                break;

            case EntityType.BOOL:
                if (isArray)
                {
                    msWriter.WriteBoolArray((bool[])val);
                }
                else
                {
                    msWriter.WriteBool((bool)val);
                }
                break;

            case EntityType.ENUM:
                if (isArray)
                {
                    Array    arr    = (Array)val;
                    string[] strarr = new string[arr.Length];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        strarr[i] = arr.GetValue(i).ToString();
                    }
                    msWriter.WriteStringArray(strarr);
                }
                else
                {
                    msWriter.WriteString(val.ToString());
                }
                break;

            case EntityType.DICTIONARY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var dicArray = (Array)val;
                    msWriter.WriteInt32(dicArray.Length);
                    for (int i = 0; i < dicArray.Length; i++)
                    {
                        Serialize(dicArray.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    //
                    IDictionary idic = (IDictionary)val;
                    //写入长度
                    msWriter.WriteInt32(idic.Count);
                    int i = 0;
                    foreach (DictionaryEntry kv in idic)
                    {
                        //object k=kv.Eval("Key");
                        //object v = kv.Eval("Value");

                        Serialize(kv.Key, msWriter);
                        Serialize(kv.Value, msWriter);
                        i++;
                    }
                }
                break;

            case EntityType.LIST:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var list = (IList)val;
                    msWriter.WriteInt32(list.Count);
                    foreach (var item in list)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            case EntityType.ARRAY:
                if (isArray)
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var listarr = (Array)val;
                    msWriter.WriteInt32(listarr.Length);
                    for (int i = 0; i < listarr.Length; i++)
                    {
                        Serialize(listarr.GetValue(i), msWriter);
                    }
                }
                else
                {
                    if (val == null)
                    {
                        msWriter.WriteInt32(-1);
                        break;
                    }
                    var arr = (Array)val;
                    msWriter.WriteInt32(arr.Length);
                    foreach (var item in arr)
                    {
                        Serialize(item, msWriter);
                    }
                }
                break;

            default:
                throw new EntityBufException("序列化错误");
            }
        }