Example #1
0
        private static void SerializeComplex(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter msWriter)
        {
            if (isArray)
            {
                var vals = (object[])val;
                int len  = -1;
                if (vals != null)
                {
                    len = vals.Length;
                }
                msWriter.WriteInt32(len);
                if (len > 0)
                {
                    foreach (object v in vals)
                    {
                        //写入标志
                        if (v != null)
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                            msWriter.WriteByte((byte)flag);
                            Serialize(v, msWriter);
                        }
                        else
                        {
                            EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                            msWriter.WriteByte((byte)flag);
                        }
                    }
                }
            }
            else
            {
                //msWriter.WriteByte(complexchar);
                //msWriter.WriteByte(complexchar);

                if (val != null)
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                    msWriter.WriteByte((byte)flag);
                    Serialize(val, msWriter);
                }
                else
                {
                    EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                    msWriter.WriteByte((byte)flag);
                }
            }
        }
Example #2
0
        private static void Serialize(object o, MemoryStreamWriter msWriter)
        {
            if (o == null)
            {
                EntityBufTypeFlag flag = EntityBufTypeFlag.VlaueNull;
                msWriter.WriteByte((byte)flag);
                return;
            }
            else
            {
                EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                msWriter.WriteByte((byte)flag);
            }

            Tuple <EntityBufType, bool> tuple = GetTypeBufType(o.GetType());

            if (tuple.Item1.EntityType != EntityType.COMPLEX)
            {
                SerializeSimple(o, tuple.Item2, tuple.Item1, msWriter);
                return;
            }

            bool isArray;
            //PropertyInfo[] props = o.GetType().GetProperties();
            var entitybuftypelist = GetTypeEntityBufType(o.GetType());
            //foreach (var tp in entitybuftypelist)
            Tuple <EntityBufType, bool> tp = null;

            for (int i = 0; i < entitybuftypelist.Count; i++)
            {
                tp = entitybuftypelist[i];
                //EntityBufType buftype = MapBufType(prop.PropertyType, out isArray);
                isArray = tp.Item2;

                object val = o.Eval(tp.Item1.Property);

                if (tp.Item1.EntityType == EntityType.COMPLEX)
                {
                    SerializeComplex(val, isArray, tp.Item1, msWriter);
                }
                else
                {
                    SerializeSimple(val, isArray, tp.Item1, msWriter);
                }
            }
        }
        private static void GenSerialize(Type type, MemoryStreamWriter msWriter)
        {
            EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;

            msWriter.WriteByte((byte)flag);

            Tuple <EntityBufType, bool> tuple = GetTypeBufType(type);
            object instance = null;

            if (tuple.Item1.EntityType != EntityType.COMPLEX)
            {
                if (tuple.Item1.Property != null && tuple.Item1.Property.PropertyInfo.DeclaringType != null)
                {
                    var cor = tuple.Item1.Property.PropertyInfo.DeclaringType.GetConstructor(new Type[0]);
                    if (cor != null)
                    {
                        instance = cor.Invoke(null);
                    }
                }
                GenSerializeSimple(instance, tuple.Item2, tuple.Item1, msWriter);
                return;
            }

            bool isArray;
            //PropertyInfo[] props = o.GetType().GetProperties();
            var entitybuftypelist = GetTypeEntityBufType(type);
            //foreach (var tp in entitybuftypelist)
            Tuple <EntityBufType, bool> tp = null;

            instance = null;

            {
                var cor = type.GetConstructor(new Type[0]);
                if (cor != null)
                {
                    instance = cor.Invoke(null);
                }
            }

            for (int i = 0; i < entitybuftypelist.Count; i++)
            {
                tp = entitybuftypelist[i];
                //EntityBufType buftype = MapBufType(prop.PropertyType, out isArray);
                isArray = tp.Item2;


                if (tp.Item1.EntityType == EntityType.COMPLEX)
                {
                    GenSerializeComplex(isArray, tp.Item1, msWriter);
                }
                else
                {
                    GenSerializeSimple(instance, isArray, tp.Item1, msWriter);
                }
            }
        }
        private static void GenSerializeComplex(bool isArray, EntityBufType bufType, MemoryStreamWriter msWriter)
        {
            if (isArray)
            {
                var len = new Random().Next(1, 3);

                msWriter.WriteInt32(len);
                if (len > 0)
                {
                    for (int i = 0; i < len; i++)
                    {
                        EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                        msWriter.WriteByte((byte)flag);
                        GenSerialize(bufType.ValueType, msWriter);
                    }
                }
            }
            else
            {
                EntityBufTypeFlag flag = EntityBufTypeFlag.Empty;
                msWriter.WriteByte((byte)flag);
                GenSerialize(bufType.ValueType, msWriter);
            }
        }
        private static void GenSerializeSimple(object instance, bool isArray, EntityBufType bufType, MemoryStreamWriter msWriter)
        {
            //if (bufType.EntityType == EntityType.COMPLEX)
            //{
            //    throw new Exception("无法序列化复杂类型");
            //}

            object defaultvalue = null;

            if (instance != null)
            {
                defaultvalue = instance.Eval(bufType.Property.PropertyInfo);
            }

            switch (bufType.EntityType)
            {
            case EntityType.BYTE:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteByteArray((byte[])defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteByteArray(ValueGen.GenBytes().ToArray());
                    }
                }
                else
                {
                    msWriter.WriteByte(ValueGen.GenByte());
                }
                break;

            case EntityType.STRING:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteStringArray((String[])defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteStringArray(ValueGen.GenStrings().ToArray());
                    }
                }
                else
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteString((string)defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteString(ValueGen.GenString());
                    }
                }
                break;

            case EntityType.SHORT:
            case EntityType.INT16:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteInt16Array((short[])defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteInt16Array(ValueGen.GenInt16s().ToArray());
                    }
                }
                else
                {
                    if (defaultvalue != null && (short)defaultvalue != default(short))
                    {
                        msWriter.WriteInt16((short)defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteInt16(ValueGen.GenInt16());
                    }
                }
                break;

            case EntityType.USHORT:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteUInt16Array((ushort[])defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteUInt16Array(ValueGen.GenUInt16s().ToArray());
                    }
                }
                else
                {
                    if (defaultvalue != null && (ushort)defaultvalue != default(ushort))
                    {
                        msWriter.WriteUInt16((ushort)defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteUInt16(ValueGen.GenUInt16());
                    }
                }
                break;

            case EntityType.INT32:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteInt32Array(ValueGen.GenInt32s().ToArray());
                    }
                    else
                    {
                        msWriter.WriteInt32Array((Int32[])defaultvalue);
                    }
                }
                else
                {
                    if (defaultvalue != null && (int)defaultvalue != default(int))
                    {
                        msWriter.WriteInt32((int)defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteInt32(ValueGen.GenInt32());
                    }
                }
                break;

            case EntityType.CHAR:
                if (isArray)
                {
                    if (defaultvalue != null)
                    {
                        msWriter.WriteCharArray((char[])defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteCharArray(ValueGen.GenChars().ToArray());
                    }
                }
                else
                {
                    if (defaultvalue != null && (char)defaultvalue != default(char))
                    {
                        msWriter.WriteChar((char)defaultvalue);
                    }
                    else
                    {
                        msWriter.WriteChar(ValueGen.GenChar());
                    }
                }
                break;

            case EntityType.DECIMAL:
                if (isArray)
                {
                    msWriter.WriteDeciamlArray(ValueGen.GenDecimals().ToArray());
                }
                else
                {
                    msWriter.WriteDecimal(ValueGen.GenDecimal());
                }
                break;

            case EntityType.DOUBLE:
                if (isArray)
                {
                    msWriter.WriteDoubleArray(ValueGen.GenDoubles().ToArray());
                }
                else
                {
                    msWriter.WriteDouble(ValueGen.GenDouble());
                }
                break;

            case EntityType.FLOAT:
                if (isArray)
                {
                    msWriter.WriteFloatArray(ValueGen.GenFloats().ToArray());
                }
                else
                {
                    msWriter.WriteFloat(ValueGen.GenFloat());
                }
                break;

            case EntityType.INT64:
                if (isArray)
                {
                    msWriter.WriteInt64Array(ValueGen.GenLongs().ToArray());
                }
                else
                {
                    msWriter.WriteInt64(ValueGen.GenLong());
                }
                break;

            case EntityType.DATETIME:
                if (isArray)
                {
                    msWriter.WriteDateTimeArray(ValueGen.GenDates().ToArray());
                }
                else
                {
                    msWriter.WriteDateTime(ValueGen.GenDate());
                }
                break;

            case EntityType.BOOL:
                if (isArray)
                {
                    msWriter.WriteBoolArray(ValueGen.GenBools().ToArray());
                }
                else
                {
                    msWriter.WriteBool(ValueGen.GenBool());
                }
                break;

            case EntityType.ENUM:
                if (isArray)
                {
                    msWriter.WriteStringArray(ValueGen.GenEnums(bufType.ValueType).ToArray());
                }
                else
                {
                    msWriter.WriteString(ValueGen.GenEnum(bufType.ValueType));
                }
                break;

            case EntityType.DICTIONARY:
                if (isArray)
                {
                    var diclen = new Random().Next(1, 3);

                    msWriter.WriteInt32(diclen);
                    for (int i = 0; i < diclen; i++)
                    {
                        GenSerialize(bufType.ValueType, msWriter);
                    }
                }
                else
                {
                    var diccount = 1;
                    //
                    //写入长度
                    msWriter.WriteInt32(diccount);
                    int i = 0;
                    while (i < diccount)
                    {
                        //object k=kv.Eval("Key");
                        //object v = kv.Eval("Value");

                        GenSerialize(bufType.GenerTypes[0], msWriter);
                        GenSerialize(bufType.GenerTypes[1], msWriter);
                        i++;
                    }
                }
                break;

            case EntityType.LIST:
                if (isArray)
                {
                    var listlen = new Random().Next(1, 3);
                    msWriter.WriteInt32(listlen);
                    for (int i = 0; i < listlen; i++)
                    {
                        GenSerialize(GetListValueType(bufType.ClassType), msWriter);
                    }
                }
                else
                {
                    var listcount = new Random().Next(1, 3);
                    msWriter.WriteInt32(listcount);
                    for (int i = 0; i < listcount; i++)
                    {
                        GenSerialize(GetListValueType(bufType.ClassType), msWriter);
                    }
                }
                break;

            case EntityType.ARRAY:
                if (isArray)
                {
                    var arrlen = new Random().Next(1, 3);

                    msWriter.WriteInt32(arrlen);
                    for (int i = 0; i < arrlen; i++)
                    {
                        GenSerialize(bufType.ClassType, msWriter);
                    }
                }
                else
                {
                    var arrlen = new Random().Next(1, 3);
                    msWriter.WriteInt32(arrlen);
                    for (int i = 0; i < arrlen; i++)
                    {
                        GenSerialize(bufType.ClassType, msWriter);
                    }
                }
                break;

            default:
                throw new EntityBufException("序列化错误");
            }
        }
Example #6
0
        private static void SerializeSimple(object val, bool isArray, EntityBufType bufType, MemoryStreamWriter 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;
                    }
                    System.Collections.IDictionary idic = (System.Collections.IDictionary)val;
                    //写入长度
                    msWriter.WriteInt32(idic.Count);
                    int i = 0;
                    foreach (var kv in idic)
                    {
                        object k = kv.Eval("Key");
                        object v = kv.Eval("Value");

                        Serialize(k, msWriter);
                        Serialize(v, 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 = (System.Collections.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("序列化错误");
            }
        }